incPureUpload.asp
上传用户:yjkj1008
上传日期:2020-10-17
资源大小:1496k
文件大小:11k
源码类别:

电子政务应用

开发平台:

HTML/CSS

  1. <SCRIPT LANGUAGE="VBSCRIPT" RUNAT="SERVER">
  2. '*** Pure ASP File Upload -----------------------------------------------------
  3. ' Copyright 2000 (c) George Petrov, www.UDzone.com
  4. '
  5. ' Script partially based on code from Philippe Collignon 
  6. '              (http://www.asptoday.com/articles/20000316.htm)
  7. '
  8. ' New features from GP:
  9. '  * Fast file save with ADO 2.5 stream object
  10. '  * new wrapper functions, extra error checking
  11. '  * UltraDev Server Behavior extension
  12. ' Copyright 2001-2002 (c) George Petrov and Modify by xPilot
  13. ' *** Date: 12/15/2001 ***
  14. ' *** 支持所有双字节文件名,而且修复了原函数中遇到空格也会自动截断文件名的错误! ***
  15. ' *** 保证百分百以原文件名保存上传文件!***
  16. ' *** Welcome to visite pilothome.yeah.net or mail xpilot@21cn.com to me!***
  17. '
  18. ' Version: 2.0.2 Beta for GB2312,BIG5,Japan,Korea ...
  19. '*** Pure ASP File Upload -----------------------------------------------------
  20. Sub BuildUploadRequest(RequestBin,UploadDirectory,storeType,sizeLimit,nameConflict)
  21.   'Get the boundary
  22.   PosBeg = 1
  23.   PosEnd = InstrB(PosBeg,RequestBin,getByteString(chr(13)))
  24.   if PosEnd = 0 then
  25.     Response.Write "<b>Form was submitted with no ENCTYPE=""multipart/form-data""</b><br>"
  26.     Response.Write "Please correct and <A HREF=""javascript:history.back(1)"">try again</a>"    
  27.     Response.End
  28.   end if
  29.   'Check ADO Version
  30. set checkADOConn = Server.CreateObject("ADODB.Connection")
  31. adoVersion = CSng(checkADOConn.Version)
  32. set checkADOConn = Nothing
  33. if adoVersion < 2.5 then
  34.     Response.Write "<b>You don't have ADO 2.5 installed on the server.</b><br>"
  35.     Response.Write "The File Upload extension needs ADO 2.5 or greater to run properly.<br>"
  36.     Response.Write "You can download the latest MDAC (ADO is included) from <a href=""www.microsoft.com/data"">www.microsoft.com/data</a><br>"
  37.     Response.End
  38. end if
  39.   'Check content length if needed
  40. Length = CLng(Request.ServerVariables("HTTP_Content_Length")) 'Get Content-Length header
  41. If "" & sizeLimit <> "" Then
  42.     sizeLimit = CLng(sizeLimit)
  43.     If Length > sizeLimit Then
  44.       Request.BinaryRead (Length)
  45.       Response.Write "Upload size " & FormatNumber(Length, 0) & "B exceeds limit of " & FormatNumber(sizeLimit, 0) & "B"
  46.       Response.Write "Please correct and <A HREF=""javascript:history.back(1)"">try again</a>"      
  47.       Response.End
  48.     End If
  49.   End If
  50.   boundary = MidB(RequestBin,PosBeg,PosEnd-PosBeg)
  51.   boundaryPos = InstrB(1,RequestBin,boundary)
  52.   'Get all data inside the boundaries
  53.   Do until (boundaryPos=InstrB(RequestBin,boundary & getByteString("--")))
  54.     'Members variable of objects are put in a dictionary object
  55.     Dim UploadControl
  56.     Set UploadControl = CreateObject("Scripting.Dictionary")
  57.     'Get an object name
  58.     Pos = InstrB(BoundaryPos,RequestBin,getByteString("Content-Disposition"))
  59.     Pos = InstrB(Pos,RequestBin,getByteString("name="))
  60.     PosBeg = Pos+6
  61.     PosEnd = InstrB(PosBeg,RequestBin,getByteString(chr(34)))
  62.     Name = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
  63.     PosFile = InstrB(BoundaryPos,RequestBin,getByteString("filename="))
  64.     PosBound = InstrB(PosEnd,RequestBin,boundary)
  65.     'Test if object is of file type
  66.     If  PosFile<>0 AND (PosFile<PosBound) Then
  67.       'Get Filename, content-type and content of file
  68.       PosBeg = PosFile + 10
  69.       PosEnd =  InstrB(PosBeg,RequestBin,getByteString(chr(34)))
  70.       FileName = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
  71.       FileName = Mid(FileName,InStrRev(FileName,"")+1)
  72.       'Add filename to dictionary object
  73.       UploadControl.Add "FileName", FileName
  74.       Pos = InstrB(PosEnd,RequestBin,getByteString("Content-Type:"))
  75.       PosBeg = Pos+14
  76.       PosEnd = InstrB(PosBeg,RequestBin,getByteString(chr(13)))
  77.       'Add content-type to dictionary object
  78.       ContentType = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
  79.       UploadControl.Add "ContentType",ContentType
  80.       'Get content of object
  81.       PosBeg = PosEnd+4
  82.       PosEnd = InstrB(PosBeg,RequestBin,boundary)-2
  83.       Value = FileName
  84.       ValueBeg = PosBeg-1
  85.       ValueLen = PosEnd-Posbeg
  86.     Else
  87.       'Get content of object
  88.       Pos = InstrB(Pos,RequestBin,getByteString(chr(13)))
  89.       PosBeg = Pos+4
  90.       PosEnd = InstrB(PosBeg,RequestBin,boundary)-2
  91.       Value = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
  92.       ValueBeg = 0
  93.       ValueEnd = 0
  94.     End If
  95.     'Add content to dictionary object
  96.     UploadControl.Add "Value" , Value
  97.     UploadControl.Add "ValueBeg" , ValueBeg
  98.     UploadControl.Add "ValueLen" , ValueLen
  99.     'Add dictionary object to main dictionary
  100.     UploadRequest.Add name, UploadControl
  101.     'Loop to next object
  102.     BoundaryPos=InstrB(BoundaryPos+LenB(boundary),RequestBin,boundary)
  103.   Loop
  104.   GP_keys = UploadRequest.Keys
  105.   for GP_i = 0 to UploadRequest.Count - 1
  106.     GP_curKey = GP_keys(GP_i)
  107.     'Save all uploaded files
  108.     if UploadRequest.Item(GP_curKey).Item("FileName") <> "" then
  109.       GP_value = UploadRequest.Item(GP_curKey).Item("Value")
  110.       GP_valueBeg = UploadRequest.Item(GP_curKey).Item("ValueBeg")
  111.       GP_valueLen = UploadRequest.Item(GP_curKey).Item("ValueLen")
  112.       if GP_valueLen = 0 then
  113.         Response.Write "<B>An error has occured saving uploaded file!</B><br><br>"
  114.         Response.Write "Filename: " & Trim(GP_curPath) & UploadRequest.Item(GP_curKey).Item("FileName") & "<br>"
  115.         Response.Write "File does not exists or is empty.<br>"
  116.         Response.Write "Please correct and <A HREF=""javascript:history.back(1)"">try again</a>"
  117.      response.End
  118.     end if
  119.       
  120.       'Create a Stream instance
  121.       Dim GP_strm1, GP_strm2
  122.       Set GP_strm1 = Server.CreateObject("ADODB.Stream")
  123.       Set GP_strm2 = Server.CreateObject("ADODB.Stream")
  124.       
  125.       'Open the stream
  126.       GP_strm1.Open
  127.       GP_strm1.Type = 1 'Binary
  128.       GP_strm2.Open
  129.       GP_strm2.Type = 1 'Binary
  130.         
  131.       GP_strm1.Write RequestBin
  132.       GP_strm1.Position = GP_ValueBeg
  133.       GP_strm1.CopyTo GP_strm2,GP_ValueLen
  134.     
  135.       'Create and Write to a File
  136.       GP_curPath = Request.ServerVariables("PATH_INFO")
  137.       GP_curPath = Trim(Mid(GP_curPath,1,InStrRev(GP_curPath,"/")) & UploadDirectory)
  138.       if Mid(GP_curPath,Len(GP_curPath),1)  <> "/" then
  139.         GP_curPath = GP_curPath & "/"
  140.       end if 
  141.       GP_CurFileName = UploadRequest.Item(GP_curKey).Item("FileName")
  142.       GP_FullPath = Trim(Server.mappath(GP_curPath))
  143.       GP_FullFileName = GP_FullPath & "" & GP_CurFileName
  144.       Set fso = CreateObject("Scripting.FileSystemObject")
  145.       'Check if the folder exist
  146.       If NOT fso.FolderExists(GP_FullPath) Then
  147.         GP_BegFolder = InStr(GP_FullPath,"")
  148.         while GP_begFolder > 0 
  149.           GP_RelFolder = Mid(GP_FullPath,1,GP_BegFolder-1)
  150.           If NOT fso.FolderExists(GP_RelFolder) Then  
  151.             fso.CreateFolder(GP_RelFolder)
  152.           end if          
  153.           GP_BegFolder = InStr(GP_BegFolder+1,GP_FullPath,"")          
  154.         wend
  155.         If NOT fso.FolderExists(GP_FullPath) Then        
  156.           fso.CreateFolder(GP_FullPath)        
  157.         end if  
  158.       end if
  159.       'Check if the file already exist
  160.       GP_FileExist = false
  161.       If fso.FileExists(GP_FullFileName) Then
  162.         GP_FileExist = true
  163.       End If      
  164.       if nameConflict = "error" and GP_FileExist then
  165.         Response.Write "<B>File already exists!</B><br><br>"
  166.         Response.Write "Please correct and <A HREF=""javascript:history.back(1)"">try again</a>"
  167. GP_strm1.Close
  168. GP_strm2.Close
  169.      response.End
  170.       end if
  171.       if ((nameConflict = "over" or nameConflict = "uniq") and GP_FileExist) or (NOT GP_FileExist) then
  172.         if nameConflict = "uniq" and GP_FileExist then
  173.           Begin_Name_Num = 0
  174.           while GP_FileExist    
  175.             Begin_Name_Num = Begin_Name_Num + 1
  176.             GP_FullFileName = Trim(Server.mappath(GP_curPath))& "" & fso.GetBaseName(GP_CurFileName) & "_" & Begin_Name_Num & "." & fso.GetExtensionName(GP_CurFileName)
  177.             GP_FileExist = fso.FileExists(GP_FullFileName)
  178.           wend  
  179.           UploadRequest.Item(GP_curKey).Item("FileName") = fso.GetBaseName(GP_CurFileName) & "_" & Begin_Name_Num & "." & fso.GetExtensionName(GP_CurFileName)
  180. UploadRequest.Item(GP_curKey).Item("Value") = UploadRequest.Item(GP_curKey).Item("FileName")
  181.         end if
  182.         on error resume next
  183.         GP_strm2.SaveToFile GP_FullFileName,2
  184.         if err then
  185.           Response.Write "<B>An error has occured saving uploaded file!</B><br><br>"
  186.           Response.Write "Filename: " & Trim(GP_curPath) & UploadRequest.Item(GP_curKey).Item("FileName") & "<br>"
  187.           Response.Write "Maybe the destination directory does not exist, or you don't have write permission.<br>"
  188.           Response.Write "Please correct and <A HREF=""javascript:history.back(1)"">try again</a>"
  189.        err.clear
  190.    GP_strm1.Close
  191.    GP_strm2.Close
  192.         response.End
  193.        end if
  194.    GP_strm1.Close
  195.    GP_strm2.Close
  196.    if storeType = "path" then
  197.    UploadRequest.Item(GP_curKey).Item("Value") = GP_curPath & UploadRequest.Item(GP_curKey).Item("Value")
  198.    end if
  199.         on error goto 0
  200.       end if
  201.     end if
  202.   next
  203. End Sub
  204. '把普通字符串转成二进制字符串函数
  205. Function getByteString(StringStr)
  206.     getByteString=""
  207.   For i = 1 To Len(StringStr) 
  208.     XP_varchar = mid(StringStr,i,1)
  209.     XP_varasc = Asc(XP_varchar) 
  210.     If XP_varasc < 0 Then 
  211.        XP_varasc = XP_varasc + 65535 
  212.     End If 
  213.     If XP_varasc > 255 Then 
  214.        XP_varlow = Left(Hex(Asc(XP_varchar)),2) 
  215.        XP_varhigh = right(Hex(Asc(XP_varchar)),2) 
  216.        getByteString = getByteString & chrB("&H" & XP_varlow) & chrB("&H" & XP_varhigh) 
  217.     Else 
  218.        getByteString = getByteString & chrB(AscB(XP_varchar)) 
  219.     End If 
  220.   Next 
  221. End Function
  222. '把二进制字符串转换成普通字符串函数 
  223. Function getString(StringBin)
  224.    getString =""
  225.    Dim XP_varlen,XP_vargetstr,XP_string,XP_skip
  226.    XP_skip = 0 
  227.    XP_string = "" 
  228.  If Not IsNull(StringBin) Then 
  229.       XP_varlen = LenB(StringBin) 
  230.     For i = 1 To XP_varlen 
  231.       If XP_skip = 0 Then
  232.          XP_vargetstr = MidB(StringBin,i,1) 
  233.          If AscB(XP_vargetstr) > 127 Then 
  234.            XP_string = XP_string & Chr(AscW(MidB(StringBin,i+1,1) & XP_vargetstr)) 
  235.            XP_skip = 1 
  236.          Else 
  237.            XP_string = XP_string & Chr(AscB(XP_vargetstr)) 
  238.          End If 
  239.       Else 
  240.       XP_skip = 0
  241.    End If 
  242.     Next 
  243.  End If 
  244.       getString = XP_string 
  245. End Function 
  246. Function UploadFormRequest(name)
  247.   on error resume next
  248.   if UploadRequest.Item(name) then
  249.     UploadFormRequest = UploadRequest.Item(name).Item("Value")
  250.   end if  
  251. End Function
  252. Sub PureUploadSetup()
  253.   UploadQueryString = Replace(Request.QueryString,"GP_upload=true","")
  254.   if mid(UploadQueryString,1,1) = "&" then
  255.    UploadQueryString = Mid(UploadQueryString,2)
  256.   end if
  257.   GP_uploadAction = CStr(Request.ServerVariables("URL")) & "?GP_upload=true"
  258.   If (Request.QueryString <> "") Then  
  259.     if UploadQueryString <> "" then
  260.      GP_uploadAction = GP_uploadAction & "&" & UploadQueryString
  261.     end if 
  262.   End If
  263. End Sub
  264. </SCRIPT>