Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

405 řádky
14KB

  1. <!--METADATA
  2. TYPE="TypeLib"
  3. NAME="Microsoft ActiveX Data Objects 2.5 Library"
  4. UUID="{00000205-0000-0010-8000-00AA006D2EA4}"
  5. VERSION="2.5"
  6. -->
  7. <%
  8. ' For examples, documentation, and your own free copy, go to:
  9. ' http://www.freeaspupload.net
  10. ' Note: You can copy and use this script for free and you can make changes
  11. ' to the code, but you cannot remove the above comment.
  12. 'Changes:
  13. 'Aug 2, 2005: Add support for checkboxes and other input elements with multiple values
  14. 'Jan 6, 2009: Lars added ASP_CHUNK_SIZE
  15. const DEFAULT_ASP_CHUNK_SIZE = 200000
  16. Class FreeASPUpload
  17. Public UploadedFiles
  18. Public FormElements
  19. Private VarArrayBinRequest
  20. Private StreamRequest
  21. Private uploadedYet
  22. Private internalChunkSize
  23. Private Sub Class_Initialize()
  24. Set UploadedFiles = Server.CreateObject("Scripting.Dictionary")
  25. Set FormElements = Server.CreateObject("Scripting.Dictionary")
  26. Set StreamRequest = Server.CreateObject("ADODB.Stream")
  27. StreamRequest.Type = 2 ' adTypeText
  28. StreamRequest.Open
  29. uploadedYet = false
  30. internalChunkSize = DEFAULT_ASP_CHUNK_SIZE
  31. End Sub
  32. Private Sub Class_Terminate()
  33. If IsObject(UploadedFiles) Then
  34. UploadedFiles.RemoveAll()
  35. Set UploadedFiles = Nothing
  36. End If
  37. If IsObject(FormElements) Then
  38. FormElements.RemoveAll()
  39. Set FormElements = Nothing
  40. End If
  41. StreamRequest.Close
  42. Set StreamRequest = Nothing
  43. End Sub
  44. Public Property Get Form(sIndex)
  45. Form = ""
  46. If FormElements.Exists(LCase(sIndex)) Then Form = FormElements.Item(LCase(sIndex))
  47. End Property
  48. Public Property Get Files()
  49. Files = UploadedFiles.Items
  50. End Property
  51. Public Property Get Exists(sIndex)
  52. Exists = false
  53. If FormElements.Exists(LCase(sIndex)) Then Exists = true
  54. End Property
  55. Public Property Get FileExists(sIndex)
  56. FileExists = false
  57. if UploadedFiles.Exists(LCase(sIndex)) then FileExists = true
  58. End Property
  59. Public Property Get chunkSize()
  60. chunkSize = internalChunkSize
  61. End Property
  62. Public Property Let chunkSize(sz)
  63. internalChunkSize = sz
  64. End Property
  65. 'Calls Upload to extract the data from the binary request and then saves the uploaded files
  66. Public Sub Save(path)
  67. Dim streamFile, fileItem
  68. if Right(path, 1) <> "\" then path = path & "\"
  69. if not uploadedYet then Upload
  70. For Each fileItem In UploadedFiles.Items
  71. Set streamFile = Server.CreateObject("ADODB.Stream")
  72. streamFile.Type = 1
  73. streamFile.Open
  74. StreamRequest.Position=fileItem.Start
  75. StreamRequest.CopyTo streamFile, fileItem.Length
  76. streamFile.SaveToFile path & fileItem.FileName, 2
  77. streamFile.close
  78. Set streamFile = Nothing
  79. fileItem.Path = path & fileItem.FileName
  80. Next
  81. End Sub
  82. public sub SaveOne(path, num, byref outFileName, byref outLocalFileName)
  83. Dim streamFile, fileItems, fileItem, fs
  84. set fs = Server.CreateObject("Scripting.FileSystemObject")
  85. if Right(path, 1) <> "\" then path = path & "\"
  86. if not uploadedYet then Upload
  87. if UploadedFiles.Count > 0 then
  88. fileItems = UploadedFiles.Items
  89. set fileItem = fileItems(num)
  90. outFileName = fileItem.FileName
  91. outLocalFileName = GetFileName(path, outFileName)
  92. Set streamFile = Server.CreateObject("ADODB.Stream")
  93. streamFile.Type = 1
  94. streamFile.Open
  95. StreamRequest.Position = fileItem.Start
  96. StreamRequest.CopyTo streamFile, fileItem.Length
  97. streamFile.SaveToFile path & outLocalFileName, 2
  98. streamFile.close
  99. Set streamFile = Nothing
  100. fileItem.Path = path & filename
  101. end if
  102. end sub
  103. Public Function SaveBinRequest(path) ' For debugging purposes
  104. StreamRequest.SaveToFile path & "\debugStream.bin", 2
  105. End Function
  106. Public Sub DumpData() 'only works if files are plain text
  107. Dim i, aKeys, f
  108. response.write "Form Items:<br>"
  109. aKeys = FormElements.Keys
  110. For i = 0 To FormElements.Count -1 ' Iterate the array
  111. response.write aKeys(i) & " = " & FormElements.Item(aKeys(i)) & "<BR>"
  112. Next
  113. response.write "Uploaded Files:<br>"
  114. For Each f In UploadedFiles.Items
  115. response.write "Name: " & f.FileName & "<br>"
  116. response.write "Type: " & f.ContentType & "<br>"
  117. response.write "Start: " & f.Start & "<br>"
  118. response.write "Size: " & f.Length & "<br>"
  119. Next
  120. End Sub
  121. Public Sub Upload()
  122. Dim nCurPos, nDataBoundPos, nLastSepPos
  123. Dim nPosFile, nPosBound
  124. Dim sFieldName, osPathSep, auxStr
  125. Dim readBytes, readLoop, tmpBinRequest
  126. 'RFC1867 Tokens
  127. Dim vDataSep
  128. Dim tNewLine, tDoubleQuotes, tTerm, tFilename, tName, tContentDisp, tContentType
  129. tNewLine = String2Byte(Chr(13))
  130. tDoubleQuotes = String2Byte(Chr(34))
  131. tTerm = String2Byte("--")
  132. tFilename = String2Byte("filename=""")
  133. tName = String2Byte("name=""")
  134. tContentDisp = String2Byte("Content-Disposition")
  135. tContentType = String2Byte("Content-Type:")
  136. uploadedYet = true
  137. on error resume next
  138. readBytes = internalChunkSize
  139. VarArrayBinRequest = Request.BinaryRead(readBytes)
  140. VarArrayBinRequest = midb(VarArrayBinRequest, 1, lenb(VarArrayBinRequest))
  141. for readLoop = 0 to 300000
  142. tmpBinRequest = Request.BinaryRead(readBytes)
  143. if readBytes < 1 then exit for
  144. VarArrayBinRequest = VarArrayBinRequest & midb(tmpBinRequest, 1, lenb(tmpBinRequest))
  145. next
  146. if Err.Number <> 0 then
  147. response.write "<br><br><B>System reported this error:</B><p>"
  148. response.write Err.Description & "<p>"
  149. response.write "The most likely cause for this error is the incorrect setup of AspMaxRequestEntityAllowed in IIS MetaBase. Please see instructions in the <A HREF='http://www.freeaspupload.net/freeaspupload/requirements.asp'>requirements page of freeaspupload.net</A>.<p>"
  150. Exit Sub
  151. end if
  152. on error goto 0 'reset error handling
  153. nCurPos = FindToken(tNewLine,1) 'Note: nCurPos is 1-based (and so is InstrB, MidB, etc)
  154. If nCurPos <= 1 Then Exit Sub
  155. 'vDataSep is a separator like -----------------------------21763138716045
  156. vDataSep = MidB(VarArrayBinRequest, 1, nCurPos-1)
  157. 'Start of current separator
  158. nDataBoundPos = 1
  159. 'Beginning of last line
  160. nLastSepPos = FindToken(vDataSep & tTerm, 1)
  161. Do Until nDataBoundPos = nLastSepPos
  162. nCurPos = SkipToken(tContentDisp, nDataBoundPos)
  163. nCurPos = SkipToken(tName, nCurPos)
  164. sFieldName = ExtractField(tDoubleQuotes, nCurPos)
  165. nPosFile = FindToken(tFilename, nCurPos)
  166. nPosBound = FindToken(vDataSep, nCurPos)
  167. If nPosFile <> 0 And nPosFile < nPosBound Then
  168. Dim oUploadFile
  169. Set oUploadFile = New UploadedFile
  170. nCurPos = SkipToken(tFilename, nCurPos)
  171. auxStr = ExtractField(tDoubleQuotes, nCurPos)
  172. ' We are interested only in the name of the file, not the whole path
  173. ' Path separator is \ in windows, / in UNIX
  174. ' While IE seems to put the whole pathname in the stream, Mozilla seem to
  175. ' only put the actual file name, so UNIX paths may be rare. But not impossible.
  176. osPathSep = "\"
  177. if InStr(auxStr, osPathSep) = 0 then osPathSep = "/"
  178. oUploadFile.FileName = Right(auxStr, Len(auxStr)-InStrRev(auxStr, osPathSep))
  179. if (Len(oUploadFile.FileName) > 0) then 'File field not left empty
  180. nCurPos = SkipToken(tContentType, nCurPos)
  181. auxStr = ExtractField(tNewLine, nCurPos)
  182. ' NN on UNIX puts things like this in the stream:
  183. ' ?? python py type=?? python application/x-python
  184. oUploadFile.ContentType = Right(auxStr, Len(auxStr)-InStrRev(auxStr, " "))
  185. nCurPos = FindToken(tNewLine, nCurPos) + 4 'skip empty line
  186. oUploadFile.Start = nCurPos+1
  187. oUploadFile.Length = FindToken(vDataSep, nCurPos) - 2 - nCurPos
  188. If oUploadFile.Length > 0 Then UploadedFiles.Add LCase(sFieldName), oUploadFile
  189. End If
  190. Else
  191. Dim nEndOfData
  192. nCurPos = FindToken(tNewLine, nCurPos) + 4 'skip empty line
  193. nEndOfData = FindToken(vDataSep, nCurPos) - 2
  194. If Not FormElements.Exists(LCase(sFieldName)) Then
  195. FormElements.Add LCase(sFieldName), Byte2String(MidB(VarArrayBinRequest, nCurPos, nEndOfData-nCurPos))
  196. else
  197. FormElements.Item(LCase(sFieldName))= FormElements.Item(LCase(sFieldName)) & ", " & Byte2String(MidB(VarArrayBinRequest, nCurPos, nEndOfData-nCurPos))
  198. end if
  199. End If
  200. 'Advance to next separator
  201. nDataBoundPos = FindToken(vDataSep, nCurPos)
  202. Loop
  203. StreamRequest.WriteText(VarArrayBinRequest)
  204. End Sub
  205. Private Function SkipToken(sToken, nStart)
  206. SkipToken = InstrB(nStart, VarArrayBinRequest, sToken)
  207. If SkipToken = 0 then
  208. Response.write "Error in parsing uploaded binary request. The most likely cause for this error is the incorrect setup of AspMaxRequestEntityAllowed in IIS MetaBase. Please see instructions in the <A HREF='http://www.freeaspupload.net/freeaspupload/requirements.asp'>requirements page of freeaspupload.net</A>.<p>"
  209. Response.End
  210. end if
  211. SkipToken = SkipToken + LenB(sToken)
  212. End Function
  213. Private Function FindToken(sToken, nStart)
  214. FindToken = InstrB(nStart, VarArrayBinRequest, sToken)
  215. End Function
  216. Private Function ExtractField(sToken, nStart)
  217. Dim nEnd
  218. nEnd = InstrB(nStart, VarArrayBinRequest, sToken)
  219. If nEnd = 0 then
  220. Response.write "Error in parsing uploaded binary request."
  221. Response.End
  222. end if
  223. ExtractField = Byte2String(MidB(VarArrayBinRequest, nStart, nEnd-nStart))
  224. End Function
  225. 'String to byte string conversion
  226. Private Function String2Byte(sString)
  227. Dim i
  228. For i = 1 to Len(sString)
  229. String2Byte = String2Byte & ChrB(AscB(Mid(sString,i,1)))
  230. Next
  231. End Function
  232. 'Byte string to string conversion
  233. Private Function Byte2String(bsString)
  234. Dim i
  235. dim b1, b2, b3, b4
  236. Byte2String =""
  237. For i = 1 to LenB(bsString)
  238. if AscB(MidB(bsString,i,1)) < 128 then
  239. ' One byte
  240. Byte2String = Byte2String & ChrW(AscB(MidB(bsString,i,1)))
  241. elseif AscB(MidB(bsString,i,1)) < 224 then
  242. ' Two bytes
  243. b1 = AscB(MidB(bsString,i,1))
  244. b2 = AscB(MidB(bsString,i+1,1))
  245. Byte2String = Byte2String & ChrW((((b1 AND 28) / 4) * 256 + (b1 AND 3) * 64 + (b2 AND 63)))
  246. i = i + 1
  247. elseif AscB(MidB(bsString,i,1)) < 240 then
  248. ' Three bytes
  249. b1 = AscB(MidB(bsString,i,1))
  250. b2 = AscB(MidB(bsString,i+1,1))
  251. b3 = AscB(MidB(bsString,i+2,1))
  252. Byte2String = Byte2String & ChrW(((b1 AND 15) * 16 + (b2 AND 60)) * 256 + (b2 AND 3) * 64 + (b3 AND 63))
  253. i = i + 2
  254. else
  255. ' Four bytes
  256. b1 = AscB(MidB(bsString,i,1))
  257. b2 = AscB(MidB(bsString,i+1,1))
  258. b3 = AscB(MidB(bsString,i+2,1))
  259. b4 = AscB(MidB(bsString,i+3,1))
  260. ' Don't know how to handle this, I believe Microsoft doesn't support these characters so I replace them with a "^"
  261. 'Byte2String = Byte2String & ChrW(((b1 AND 3) * 64 + (b2 AND 63)) & "," & (((b1 AND 28) / 4) * 256 + (b1 AND 3) * 64 + (b2 AND 63)))
  262. Byte2String = Byte2String & "^"
  263. i = i + 3
  264. end if
  265. Next
  266. End Function
  267. End Class
  268. Class UploadedFile
  269. Public ContentType
  270. Public Start
  271. Public Length
  272. Public Path
  273. Private nameOfFile
  274. ' Need to remove characters that are valid in UNIX, but not in Windows
  275. Public Property Let FileName(fN)
  276. nameOfFile = fN
  277. nameOfFile = SubstNoReg(nameOfFile, "\", "_")
  278. nameOfFile = SubstNoReg(nameOfFile, "/", "_")
  279. nameOfFile = SubstNoReg(nameOfFile, ":", "_")
  280. nameOfFile = SubstNoReg(nameOfFile, "*", "_")
  281. nameOfFile = SubstNoReg(nameOfFile, "?", "_")
  282. nameOfFile = SubstNoReg(nameOfFile, """", "_")
  283. nameOfFile = SubstNoReg(nameOfFile, "<", "_")
  284. nameOfFile = SubstNoReg(nameOfFile, ">", "_")
  285. nameOfFile = SubstNoReg(nameOfFile, "|", "_")
  286. End Property
  287. Public Property Get FileName()
  288. FileName = nameOfFile
  289. End Property
  290. 'Public Property Get FileN()ame
  291. End Class
  292. ' Does not depend on RegEx, which is not available on older VBScript
  293. ' Is not recursive, which means it will not run out of stack space
  294. Function SubstNoReg(initialStr, oldStr, newStr)
  295. Dim currentPos, oldStrPos, skip
  296. If IsNull(initialStr) Or Len(initialStr) = 0 Then
  297. SubstNoReg = ""
  298. ElseIf IsNull(oldStr) Or Len(oldStr) = 0 Then
  299. SubstNoReg = initialStr
  300. Else
  301. If IsNull(newStr) Then newStr = ""
  302. currentPos = 1
  303. oldStrPos = 0
  304. SubstNoReg = ""
  305. skip = Len(oldStr)
  306. Do While currentPos <= Len(initialStr)
  307. oldStrPos = InStr(currentPos, initialStr, oldStr)
  308. If oldStrPos = 0 Then
  309. SubstNoReg = SubstNoReg & Mid(initialStr, currentPos, Len(initialStr) - currentPos + 1)
  310. currentPos = Len(initialStr) + 1
  311. Else
  312. SubstNoReg = SubstNoReg & Mid(initialStr, currentPos, oldStrPos - currentPos) & newStr
  313. currentPos = oldStrPos + skip
  314. End If
  315. Loop
  316. End If
  317. End Function
  318. Function GetFileName(strSaveToPath, FileName)
  319. 'This function is used when saving a file to check there is not already a file with the same name so that you don't overwrite it.
  320. 'It adds numbers to the filename e.g. file.gif becomes file1.gif becomes file2.gif and so on.
  321. 'It keeps going until it returns a filename that does not exist.
  322. 'You could just create a filename from the ID field but that means writing the record - and it still might exist!
  323. 'N.B. Requires strSaveToPath variable to be available - and containing the path to save to
  324. Dim Counter
  325. Dim Flag
  326. Dim strTempFileName
  327. Dim FileExt
  328. Dim NewFullPath
  329. dim objFSO, p
  330. Set objFSO = CreateObject("Scripting.FileSystemObject")
  331. Counter = 0
  332. p = instrrev(FileName, ".")
  333. FileExt = mid(FileName, p+1)
  334. strTempFileName = left(FileName, p-1)
  335. NewFullPath = strSaveToPath & "\" & FileName
  336. Flag = False
  337. Do Until Flag = True
  338. If objFSO.FileExists(NewFullPath) = False Then
  339. Flag = True
  340. GetFileName = Mid(NewFullPath, InstrRev(NewFullPath, "\") + 1)
  341. Else
  342. Counter = Counter + 1
  343. NewFullPath = strSaveToPath & "\" & strTempFileName & Counter & "." & FileExt
  344. End If
  345. Loop
  346. End Function
  347. %>

Powered by TurnKey Linux.