123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495 |
- <%Checklogin("")
- response.charset="UTF-8"
- dim inputname,immediate,attachdir,dirtype,maxattachsize,upext,msgtype
- inputname="filedata"
- attachdir="u"
- dirtype=2
- maxattachsize=5242880
- upext="txt,rar,zip,jpg,jpeg,gif,png,swf,wmv,avi,wma,mp3,mid,pdf"
- msgtype=2
- immediate=Request.QueryString("immediate")
- id=Request.QueryString("id")
- 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 1
- attach_subdir="day_"+DateFormat(now,"yymmdd")
- case 2
- attach_subdir="m"+DateFormat(now,"yymm")
- case 3
- attach_subdir="ext_"+extension
- end select
- attach_dir=attachdir+"/"+attach_subdir+"/"
-
- 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,"ddhhnnss")+cstr(cint(99*Rnd))+"."+extension
- target=attach_dir+filename
- moveFile attach_dir+tmpfile,target
- if immediate="1" then target="!"+target
- imgurl = target
- target=jsonString(target)
- if msgtype=1 then
- msg="'"+target+"'"
- else
- msg="{'url':'"+target+"','localname':'"+jsonString(upfile.file(inputname).FileName)+"','id':'1'}"
- end if
- end if
- end if
- set upfile=nothing
- act = Request.QueryString("act")
- If act = "s" Then
- Response.Write "<script>parent.document.getElementById("""&id&""").value='/System/"&imgurl&"';location.href='imgUpload.asp'</script>"
- Else
- response.write "{'err':'"+jsonString(err)+"','msg':"+msg+"}"
- End If
- 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
-
- Class UpFile_Class
- Dim Form,File
- Dim AllowExt_
- Dim NoAllowExt_
- Dim IsDebug_
- Private oUpFileStream
- Private isErr_
- Private ErrMessage_
- Private isGetData_
- Public Property Get Version
- Version="无惧上传类 Version V2.0"
- End Property
- Public Property Get isErr
- 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
- Private Sub Class_Initialize
- isErr_ = 0
- NoAllowExt=""
- NoAllowExt=LCase(NoAllowExt)
- AllowExt=""
- AllowExt=LCase(AllowExt)
- isGetData_=false
- End Sub
- Private Sub Class_Terminate
- on error Resume Next
-
- Form.RemoveAll
- Set Form = Nothing
- File.RemoveAll
- Set File = Nothing
- oUpFileStream.Close
- Set oUpFileStream = Nothing
- if Err.number<>0 then OutErr("清除类时发生错误!")
- End Sub
- Public Sub GetData (MaxSize)
-
- on error Resume Next
- if isGetData_=false then
- Dim RequestBinData,sSpace,bCrLf,sInfo,iInfoStart,iInfoEnd,tStream,iStart,oFileInfo
- Dim sFormValue,sFileName
- Dim iFindStart,iFindEnd
- Dim iFormStart,iFormEnd,sFormName
-
- If Request.TotalBytes < 1 Then
- isErr_ = 1
- ErrMessage_="没有数据上传,这是因为直接提交网址所产生的错误!"
- OutErr("没有数据上传,这是因为直接提交网址所产生的错误!!")
- Exit Sub
- End If
- If MaxSize > 0 Then
- If Request.TotalBytes > MaxSize Then
- isErr_ = 2
- ErrMessage_="上传的数据超出限制大小!"
- OutErr("上传的数据超出限制大小!")
- Exit Sub
- End If
- End If
- Set Form = Server.CreateObject ("Scripting.Dictionary")
- Form.CompareMode = 1
- Set File = Server.CreateObject ("Scripting.Dictionary")
- File.CompareMode = 1
- Set tStream = Server.CreateObject ("ADODB.Stream")
- Set oUpFileStream = Server.CreateObject ("ADODB.Stream")
- if Err.number<>0 then OutErr("创建流对象(ADODB.STREAM)时出错,可能系统不支持或没有开通该组件")
- oUpFileStream.Type = 1
- oUpFileStream.Mode = 3
- oUpFileStream.Open
- oUpFileStream.Write Request.BinaryRead (Request.TotalBytes)
- oUpFileStream.Position = 0
- RequestBinData = oUpFileStream.Read
- Dim sHtml5FileInfo
- sHtml5FileInfo=Request.ServerVariables("HTTP_CONTENT_DISPOSITION")
- If sHtml5FileInfo<>"" Then
- iFindStart = InStr (1,sHtml5FileInfo,"name=""",1)+6
- iFindEnd = InStr (iFindStart,sHtml5FileInfo,"""",1)
- sFormName=Trim(Mid(sHtml5FileInfo,iFindStart,iFindEnd-iFindStart))
- iFindStart = InStr (iFindStart,sHtml5FileInfo,"filename=""",1)+10
- iFindEnd = InStr (iFindStart,sHtml5FileInfo,"""",1)
- sFileName = Trim(Mid(sHtml5FileInfo,iFindStart,iFindEnd-iFindStart))
- Set oFileInfo = new FileInfo_Class
- oFileInfo.FileName = GetFileName(sFileName)
- oFileInfo.FilePath = GetFilePath(sFileName)
- oFileInfo.FileExt = GetFileExt(sFileName)
- oFileInfo.FileStart = 0
- oFileInfo.FileSize = Request.TotalBytes
- oFileInfo.FormName = sFormName
- file.add sFormName,oFileInfo
- Else
- iFormEnd = oUpFileStream.Size
- bCrLf = ChrB (13) & ChrB (10)
-
- sSpace = MidB (RequestBinData,1, InStrB (1,RequestBinData,bCrLf)-1)
- iStart = LenB(sSpace)
- iFormStart = iStart+2
-
- Do
- iInfoEnd = InStrB (iFormStart,RequestBinData,bCrLf & bCrLf)+3
- tStream.Type = 1
- tStream.Mode = 3
- tStream.Open
- oUpFileStream.Position = iFormStart
- oUpFileStream.CopyTo tStream,iInfoEnd-iFormStart
- tStream.Position = 0
- tStream.Type = 2
- tStream.CharSet = "utf-8"
- sInfo = tStream.ReadText
-
- iFormStart = InStrB (iInfoEnd,RequestBinData,sSpace)-1
- iFindStart = InStr (22,sInfo,"name=""",1)+6
- iFindEnd = InStr (iFindStart,sInfo,"""",1)
- sFormName = Mid(sinfo,iFindStart,iFindEnd-iFindStart)
-
- If InStr (45,sInfo,"filename=""",1) > 0 Then
- Set oFileInfo = new FileInfo_Class
-
- iFindStart = InStr (iFindEnd,sInfo,"filename=""",1)+10
- iFindEnd = InStr (iFindStart,sInfo,""""&vbCrLf,1)
- sFileName = Trim(Mid(sinfo,iFindStart,iFindEnd-iFindStart))
- oFileInfo.FileName = GetFileName(sFileName)
- oFileInfo.FilePath = GetFilePath(sFileName)
- oFileInfo.FileExt = GetFileExt(sFileName)
- iFindStart = InStr (iFindEnd,sInfo,"Content-Type: ",1)+14
- iFindEnd = InStr (iFindStart,sInfo,vbCr)
- oFileInfo.FileMIME = Mid(sinfo,iFindStart,iFindEnd-iFindStart)
- oFileInfo.FileStart = iInfoEnd
- oFileInfo.FileSize = iFormStart -iInfoEnd -2
- oFileInfo.FormName = sFormName
- file.add sFormName,oFileInfo
- else
-
- tStream.Close
- tStream.Type = 1
- tStream.Mode = 3
- tStream.Open
- oUpFileStream.Position = iInfoEnd
- oUpFileStream.CopyTo tStream,iFormStart-iInfoEnd-2
- tStream.Position = 0
- tStream.Type = 2
- tStream.CharSet = "utf-8"
- sFormValue = tStream.ReadText
- If Form.Exists (sFormName) Then
- Form (sFormName) = Form (sFormName) & ", " & sFormValue
- else
- Form.Add sFormName,sFormValue
- End If
- End If
- tStream.Close
- iFormStart = iFormStart+iStart+2
-
- Loop Until (iFormStart+2) >= iFormEnd
- if Err.number<>0 then OutErr("分解上传数据时发生错误,可能客户端的上传数据不正确或不符合上传数据规则")
- End if
- RequestBinData = ""
- Set tStream = Nothing
- isGetData_=true
- end if
- End Sub
- Public Function SaveToFile(Item,Path)
- SaveToFile=SaveToFileEx(Item,Path,True)
- End Function
- Public Function AutoSave(Item,Path)
- AutoSave=SaveToFileEx(Item,Path,false)
- End Function
- Private Function SaveToFileEx(Item,Path,Over)
- On Error Resume Next
- Dim FileExt
- if file.Exists(Item) then
- Dim oFileStream
- Dim tmpPath
- isErr_=0
- Set oFileStream = CreateObject ("ADODB.Stream")
- oFileStream.Type = 1
- oFileStream.Mode = 3
- oFileStream.Open
- oUpFileStream.Position = File(Item).FileStart
- oUpFileStream.CopyTo oFileStream,File(Item).FileSize
- tmpPath=Split(Path,".")(0)
- FileExt=GetFileExt(Path)
- if Over then
- if isAllowExt(FileExt) then
- oFileStream.SaveToFile tmpPath & "." & FileExt,2
- if Err.number<>0 then OutErr("保存文件时出错,请检查路径,是否存在该上传目录!该文件保存路径为" & tmpPath & "." & FileExt)
- Else
- isErr_=3
- ErrMessage_="该后缀名的文件不允许上传!"
- OutErr("该后缀名的文件不允许上传")
- End if
- Else
- Path=GetFilePath(Path)
- dim fori
- fori=1
- if isAllowExt(File(Item).FileExt) then
- do
- fori=fori+1
- Err.Clear()
- tmpPath=Path&GetNewFileName()&"."&File(Item).FileExt
- oFileStream.SaveToFile tmpPath
- loop Until ((Err.number=0) or (fori>50))
- if Err.number<>0 then OutErr("自动保存文件出错,已经测试50次不同的文件名来保存,请检查目录是否存在!该文件最后一次保存时全路径为"&Path&GetNewFileName()&"."&File(Item).FileExt)
- Else
- isErr_=3
- ErrMessage_="该后缀名的文件不允许上传!"
- OutErr("该后缀名的文件不允许上传")
- End if
- End if
- oFileStream.Close
- Set oFileStream = Nothing
- else
- ErrMessage_="不存在该对象(如该文件没有上传,文件为空)!"
- OutErr("不存在该对象(如该文件没有上传,文件为空)")
- end if
- if isErr_=3 then SaveToFileEx="" else SaveToFileEx=GetFileName(tmpPath)
- End Function
- Public Function FileData(Item)
- isErr_=0
- if file.Exists(Item) then
- if isAllowExt(File(Item).FileExt) then
- oUpFileStream.Position = File(Item).FileStart
- FileData = oUpFileStream.Read (File(Item).FileSize)
- Else
- isErr_=3
- ErrMessage_="该后缀名的文件不允许上传"
- OutErr("该后缀名的文件不允许上传")
- FileData=""
- End if
- else
- ErrMessage_="不存在该对象(如该文件没有上传,文件为空)!"
- OutErr("不存在该对象(如该文件没有上传,文件为空)")
- end if
- End Function
- Public function GetFilePath(FullPath)
- If FullPath <> "" Then
- GetFilePath = Left(FullPath,InStrRev(FullPath, "\"))
- Else
- GetFilePath = ""
- End If
- End function
- Public Function GetFileName(FullPath)
- If FullPath <> "" Then
- GetFileName = mid(FullPath,InStrRev(FullPath, "\")+1)
- Else
- GetFileName = ""
- End If
- End function
- Public Function GetFileExt(FullPath)
- If FullPath <> "" Then
- GetFileExt = LCase(Mid(FullPath,InStrRev(FullPath, ".")+1))
- Else
- GetFileExt = ""
- End If
- End function
- Public Function GetNewFileName()
- dim ranNum
- dim dtNow
- dtNow=Now()
- randomize
- ranNum=int(90000*rnd)+10000
-
- GetNewFileName=year(dtNow) & right("0" & month(dtNow),2) & right("0" & day(dtNow),2) & right("0" & hour(dtNow),2) & right("0" & minute(dtNow),2) & right("0" & second(dtNow),2) & ranNum
- End Function
- Public Function isAllowExt(Ext)
- if NoAllowExt="" then
- isAllowExt=cbool(InStr(1,";"&AllowExt&";",LCase(";"&Ext&";")))
- else
- isAllowExt=not CBool(InStr(1,";"&NoAllowExt&";",LCase(";"&Ext&";")))
- end if
- End Function
- End Class
- Public Sub OutErr(ErrMsg)
- if IsDebug_=true then
- Response.Write ErrMsg
- Response.End
- End if
- End Sub
- Class FileInfo_Class
- Dim FormName,FileName,FilePath,FileSize,FileMIME,FileStart,FileExt
- End Class
- %>
|