无惧上传类上传图片的时候怎么保持图片名称不变

miduji 2013-03-21 09:15:48
<!--#include file="../Cs/Cs.Asp"-->
<%
If CheckAdminLogin(S_AdminName,S_AdminPassWord)=False Then Response.End()
If EnableUploadFile=0 Then Response.Write "<font color=red>未启用文件上传功能!</font>":Response.End()

SaveUpFilesPath=Left(SaveUpFilesPath,Len(SaveUpFilesPath)-1)
UpFileType=Replace(UpFileType," ","")
dim inputname,immediate,attachdir,dirtype,maxattachsize,upext,msgtype
inputname="filedata"'表单文件域name
attachdir=SaveUpFilesPath'上传文件保存路径,结尾不要带/
dirtype=0'0:直接保存不需要创建文件夹'1:按天存入目录 2:按月存入目录 3:按扩展名存目录 建议使用按天存
maxattachsize=MaxFileSize*1024'最大上传大小,默认是2M
upext=UpFileType'上传扩展名
msgtype=2'返回上传参数的格式:1,只返回url,2,返回参数数组
immediate=Request.QueryString("immediate")'立即上传模式,仅为演示用

if immediate="21" then '单图片文件调用接口返回函数 By Debill 2010-11-5
D_formname=Request.QueryString("D_formname")
D_inputname=Request.QueryString("D_inputname")
msgtype=21
upext="jpg,jpeg,gif,png"'上传扩展名
end if

dim err,msg,upfile
err = ""
msg = "''"

set upfile=new upfile_class
upfile.AllowExt=replace(upext,",",";")+";"
upfile.GetData(maxattachsize)
if upfile.isErr then
select case upfile.isErr
case 1
err="无数据提交"
case 2
err="文件大小超过 "+cstr(maxattachsize)+"字节"
case else
err=upfile.ErrMessage
end select
else
dim attach_dir,attach_subdir,filename,extension,target,tmpfile
extension=upfile.file(inputname).FileExt
select case dirtype
case 0
attach_dir=attachdir+"/"
case 1
attach_subdir="day_"+DateFormat(now,"yymmdd")
attach_dir=attachdir+"/"+attach_subdir+"/"
case 2
attach_subdir="month_"+DateFormat(now,"yymm")
attach_dir=attachdir+"/"+attach_subdir+"/"
case 3
attach_subdir="ext_"+extension
attach_dir=attachdir+"/"+attach_subdir+"/"
end select

'建文件夹
CreateFolder attach_dir
tmpfile=upfile.AutoSave(inputname,Server.mappath(attach_dir)+"\")
if upfile.isErr then
if upfile.isErr=3 then
err="上传文件扩展名必需为:"+upext
else
err=upfile.ErrMessage
end if
else
'生成随机文件名并改名
Randomize timer
filename=DateFormat(now,"yyyymmddhhnnss")+cstr(cint(9999*Rnd))+"."+extension
target=attach_dir+filename
moveFile attach_dir+tmpfile,target
if immediate="1" then target="!"+target
target=jsonString(target)
if msgtype=1 then
msg="'"+target+"'"
elseif msgtype=21 then '单文件调用接口返回函数 By Debill 2010-11-5
returnupfileurl=attachdir&"/"&filename
'Response.Write returnupfileurl
Response.Write "<script type=""text/javascript"">" &vbnewline
Response.Write "window.parent.document.getElementById('"&D_formname&"')."&D_inputname&".value='"&returnupfileurl&"';" &vbnewline
If D_formname="Products" Then
Response.Write "window.parent.document.getElementById('pic_view').src='"&returnupfileurl&"';" &vbnewline
Response.Write "window.parent.document.getElementById('pic_view').style.visibility='visible';" &vbnewline
End If
Response.Write "alert('上传成功!');javascript:history.go(-1);" &vbnewline
Response.Write "</script>"
response.end()
else
msg="{'url':'"+target+"','localname':'"+jsonString(upfile.file(inputname).FileName)+"','id':'1'}"
end if
end if
end if
set upfile=nothing

'单文件调用接口返回函数 By Debill 2010-11-5
if msgtype=21 then
response.write"<SCRIPT language=JavaScript>alert('"&err&"');javascript:history.go(-1);</SCRIPT>"
response.End
end if

response.write "{'err':'"+jsonString(err)+"','msg':"+msg+"}"

function jsonString(str)
str=replace(str,"\","\\")
str=replace(str,"/","\/")
str=replace(str,"'","\'")
jsonString=str
end function

Function Iif(expression,returntrue,returnfalse)
If expression=true Then
iif=returntrue
Else
iif=returnfalse
End If
End Function

function DateFormat(strDate,fstr)
if isdate(strDate) then
dim i,temp
temp=replace(fstr,"yyyy",DatePart("yyyy",strDate))
temp=replace(temp,"yy",mid(DatePart("yyyy",strDate),3))
temp=replace(temp,"y",DatePart("y",strDate))
temp=replace(temp,"w",DatePart("w",strDate))
temp=replace(temp,"ww",DatePart("ww",strDate))
temp=replace(temp,"q",DatePart("q",strDate))
temp=replace(temp,"mm",iif(len(DatePart("m",strDate))>1,DatePart("m",strDate),"0"&DatePart("m",strDate)))
temp=replace(temp,"dd",iif(len(DatePart("d",strDate))>1,DatePart("d",strDate),"0"&DatePart("d",strDate)))
temp=replace(temp,"hh",iif(len(DatePart("h",strDate))>1,DatePart("h",strDate),"0"&DatePart("h",strDate)))
temp=replace(temp,"nn",iif(len(DatePart("n",strDate))>1,DatePart("n",strDate),"0"&DatePart("n",strDate)))
temp=replace(temp,"ss",iif(len(DatePart("s",strDate))>1,DatePart("s",strDate),"0"&DatePart("s",strDate)))
DateFormat=temp
else
DateFormat=false
end if
end function

Function CreateFolder(FolderPath)
dim lpath,fs,f
lpath=Server.MapPath(FolderPath)
Set fs=Server.CreateObject("Scri"&"pting.File"&"Sys"&"temObject")
If not fs.FolderExists(lpath) then
Set f=fs.CreateFolder(lpath)
CreateFolder=F.Path
end if
Set F=Nothing
Set fs=Nothing
End Function

Function moveFile(oldfile,newfile)
dim fs
Set fs=Server.CreateObject("Scri"&"pting.File"&"Sys"&"temObject")
fs.movefile Server.MapPath(oldfile),Server.MapPath(newfile)
Set fs=Nothing
End Function

'----------------------------------------------------------------------
'转发时请保留此声明信息,这段声明不并会影响你的速度!
'******************* 无惧上传类 V2.2 xheditor特别修改版 ************************************
'作者:梁无惧
'网站:http://www.25cn.com
'电子邮件:yjlrb@21cn.com
'版权声明:版权所有,源代码公开,各种用途均可免费使用,但是修改后必须把修改后的文件
'发送一份给作者.并且保留作者此版权信息
'**********************************************************************
'----------------------------------------------------------------------
'----------------------------------------------------------------------
'文件上传类
Class UpFile_Class

Dim Form,File
Dim AllowExt_ '允许上传类型(白名单)
Dim NoAllowExt_ '不允许上传类型(黑名单)
Dim IsDebug_ '是否显示出错信息
Private oUpFileStream '上传的数据流
Private isErr_ '错误的代码,0或true表示无错
Private ErrMessage_ '错误的字符串信息
Private isGetData_ '指示是否已执行过GETDATA过程

'------------------------------------------------------------------
'类的属性
Public Property Get Version
Version="无惧上传类 Version V2.0"
End Property

Public Property Get isErr '错误的代码,0或true表示无错
isErr=isErr_
End Property

Public Property Get ErrMessage '错误的字符串信息
ErrMessage=ErrMessage_
End Property

Public Property Get AllowExt '允许上传类型(白名单)
AllowExt=AllowExt_
End Property

Public Property Let AllowExt(Value) '允许上传类型(白名单)
AllowExt_=LCase(Value)
End Property

Public Property Get NoAllowExt '不允许上传类型(黑名单)
NoAllowExt=NoAllowExt_
End Property

Public Property Let NoAllowExt(Value) '不允许上传类型(黑名单)
NoAllowExt_=LCase(Value)
End Property

Public Property Let IsDebug(Value) '是否设置为调试模式
IsDebug_=Value
End Property
...全文
74 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

3,424

社区成员

发帖
与我相关
我的任务
社区描述
其他开发语言 其他开发语言
社区管理员
  • 其他开发语言社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧