Upload.asp.cls 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. <%
  2. '=========================================================
  3. 'Class: AnUpLoad
  4. 'Author: Anlige
  5. 'Version:AienAspUpload V13.12.09
  6. 'CreationDate: 2008-04-12
  7. 'ModificationDate: 2013-12-09
  8. 'Homepage: http://dev.mo.cn
  9. 'Email: zhanghuiguoanlige@126.com
  10. 'QQ: 1034555083
  11. '=========================================================
  12. Dim StreamT
  13. Class AnUpLoad
  14. Private Form, Fils
  15. Private vCharSet, vMaxSize, vSingleSize, vErr, vVersion, vTotalSize, vExe, vErrExe,vboundary, vLostTime, vMode, vFileCount,StreamOpened
  16. private vMuti,vServerVersion
  17. Public Property Let Mode(ByVal value)
  18. vMode = value
  19. End Property
  20. Public Property Let MaxSize(ByVal value)
  21. vMaxSize = value
  22. End Property
  23. Public Property Let SingleSize(ByVal value)
  24. vSingleSize = value
  25. End Property
  26. Public Property Let Exe(ByVal value)
  27. vExe = LCase(value)
  28. vExe = replace(vExe,"*.","")
  29. vExe = replace(vExe,";","|")
  30. End Property
  31. Public Property Let CharSet(ByVal value)
  32. vCharSet = value
  33. End Property
  34. Public Property Get ErrorID()
  35. ErrorID = vErr
  36. End Property
  37. Public Property Get FileCount()
  38. FileCount = Fils.count
  39. End Property
  40. Public Property Get Description()
  41. Description = GetErr(vErr)
  42. End Property
  43. Public Property Get Version()
  44. Version = vVersion
  45. End Property
  46. Public Property Get TotalSize()
  47. TotalSize = vTotalSize
  48. End Property
  49. Public Property Get LostTime()
  50. LostTime = vLostTime
  51. End Property
  52. Private Sub Class_Initialize()
  53. set Form = server.createobject("Scripting.Dictionary")
  54. set Fils = server.createobject("Scripting.Dictionary")
  55. Set StreamT = server.CreateObject("Adodb.stream")
  56. vVersion = "AienAspUpload V13.12.09"
  57. vMaxSize = -1
  58. vSingleSize = -1
  59. vErr = -1
  60. vExe = ""
  61. vTotalSize = 0
  62. vCharSet = "gb2312"
  63. vMode = 0
  64. StreamOpened=false
  65. vMuti="_" & Getname() & "_"
  66. vServerVersion = 6.0
  67. Dim t_
  68. t_ = lcase(Request.ServerVariables("SERVER_SOFTWARE"))
  69. t_ = replace(t_,"microsoft-iis/","")
  70. if isnumeric(t_) then vServerVersion = cdbl(t_)
  71. End Sub
  72. Private Sub Class_Terminate()
  73. Dim f
  74. Form.RemoveAll()
  75. For each f in Fils
  76. Fils(f).value=empty
  77. Set Fils(f) = Nothing
  78. Next
  79. Fils.RemoveAll()
  80. Set Form = Nothing
  81. Set Fils = Nothing
  82. if StreamOpened then StreamT.close()
  83. Set StreamT = Nothing
  84. End Sub
  85. Public Sub GetData()
  86. Dim time1
  87. time1 = timer()
  88. Dim value, str, bcrlf, fpos, sSplit, slen, istart,ef
  89. Dim TotalBytes,tempdata,BytesRead,ChunkReadSize,PartSize,DataPart,formend, formhead, startpos, endpos, formname, FileName, fileExe, valueend, NewName,localname,type_1,contentType
  90. TotalBytes = Request.TotalBytes
  91. ef = false
  92. If checkEntryType = false Then ef = true : vErr = 2
  93. If vServerVersion>=6 Then
  94. If Not ef Then
  95. If vMaxSize > 0 And TotalBytes > vMaxSize Then ef = true : vErr = 1
  96. End If
  97. End If
  98. If ef Then Exit Sub
  99. If vMode = 0 Then
  100. vTotalSize = 0
  101. StreamT.Type = 1
  102. StreamT.Mode = 3
  103. StreamT.Open
  104. StreamOpened = true
  105. BytesRead = 0
  106. ChunkReadSize = 1024 * 16
  107. Do While BytesRead < TotalBytes
  108. PartSize = ChunkReadSize
  109. If PartSize + BytesRead > TotalBytes Then PartSize = TotalBytes - BytesRead
  110. DataPart = Request.BinaryRead(PartSize)
  111. StreamT.Write DataPart
  112. BytesRead = BytesRead + PartSize
  113. Loop
  114. StreamT.Position = 0
  115. tempdata = StreamT.Read
  116. Else
  117. tempdata = Request.BinaryRead(TotalBytes)
  118. End If
  119. bcrlf = ChrB(13) & ChrB(10)
  120. fpos = InStrB(1, tempdata, bcrlf)
  121. sSplit = MidB(tempdata, 1, fpos - 1)
  122. slen = LenB(sSplit)
  123. istart = slen + 2
  124. Do
  125. formend = InStrB(istart, tempdata, bcrlf & bcrlf)
  126. if formend<=0 then exit do
  127. formhead = MidB(tempdata, istart, formend - istart)
  128. str = Bytes2Str(formhead)
  129. startpos = InStr(str, "name=""") + 6
  130. if startpos<=0 then exit do
  131. endpos = InStr(startpos, str, """")
  132. if endpos<=0 then exit do
  133. formname = LCase(Mid(str, startpos, endpos - startpos))
  134. valueend = InStrB(formend + 3, tempdata, sSplit)
  135. if valueend<=0 then exit do
  136. If InStr(str, "filename=""") > 0 Then
  137. formname = formname & vMuti & "0"
  138. startpos = InStr(str, "filename=""") + 10
  139. endpos = InStr(startpos, str, """")
  140. type_1=instr(endpos,lcase(str),"content-type")
  141. contentType=trim(mid(str,type_1+13))
  142. FileName = Mid(str, startpos, endpos - startpos)
  143. If Trim(FileName) <> "" Then
  144. FileName = Replace(FileName, "/", "\")
  145. FileName = Replace(FileName, chr(0), "")
  146. LocalName = FileName
  147. FileName = Mid(FileName, InStrRev(FileName, "\") + 1)
  148. If instr(FileName,".")>0 Then
  149. fileExe = Split(FileName, ".")(UBound(Split(FileName, ".")))
  150. else
  151. fileExe = ""
  152. End If
  153. If vExe <> "" Then
  154. If checkExe(fileExe) = True Then
  155. vErr = 3
  156. vErrExe = fileExe
  157. tempdata = empty
  158. Exit Sub
  159. End If
  160. End If
  161. NewName = Getname()
  162. NewName = NewName & "." & fileExe
  163. vTotalSize = vTotalSize + valueend - formend - 6
  164. If vSingleSize > 0 And (valueend - formend - 6) > vSingleSize Then
  165. vErr = 5
  166. tempdata = empty
  167. Exit Sub
  168. End If
  169. If vMaxSize > 0 And vTotalSize > vMaxSize Then
  170. vErr = 1
  171. tempdata = empty
  172. Exit Sub
  173. End If
  174. If Fils.Exists(formname) Then formname = GetNextFormName(formname)
  175. Dim fileCls:set fileCls= new UploadFileEx
  176. fileCls.ContentType=contentType
  177. fileCls.Size = (valueend - formend - 6)
  178. fileCls.Position = (formend + 3)
  179. fileCls.FormName = formname
  180. fileCls.NewName = NewName
  181. fileCls.FileName = FileName
  182. fileCls.LocalName = FileName
  183. fileCls.extend=split(NewName,".")(ubound(split(NewName,".")))
  184. Fils.Add formname, fileCls
  185. Set fileCls = Nothing
  186. End If
  187. Else
  188. value = MidB(tempdata, formend + 4, valueend - formend - 6)
  189. If Form.Exists(formname) Then
  190. Form(formname) = Form(formname) & "," & Bytes2Str(value)
  191. Else
  192. Form.Add formname, Bytes2Str(value)
  193. End If
  194. End If
  195. istart = valueend + 2 + slen
  196. Loop Until (istart + 2) >= LenB(tempdata)
  197. vErr = 0
  198. tempdata = empty
  199. vLostTime = FormatNumber((timer-time1)*1000,2)
  200. End Sub
  201. Private Function CheckExe(ByVal ex)
  202. Dim notIn: notIn = True
  203. If vExe="*" then
  204. notIn=false
  205. elseIf InStr(1, vExe, "|") > 0 Then
  206. Dim tempExe: tempExe = Split(vExe, "|")
  207. Dim I: I = 0
  208. For I = 0 To UBound(tempExe)
  209. If LCase(ex) = tempExe(I) Then
  210. notIn = False
  211. Exit For
  212. End If
  213. Next
  214. Else
  215. If vExe = LCase(ex) Then
  216. notIn = False
  217. End If
  218. End If
  219. checkExe = notIn
  220. End Function
  221. Public Function GetSize(ByVal Size)
  222. If Size < 1024 Then
  223. GetSize = FormatNumber(Size, 2) & "B"
  224. ElseIf Size >= 1024 And Size < 1048576 Then
  225. GetSize = FormatNumber(Size / 1024, 2) & "KB"
  226. ElseIf Size >= 1048576 Then
  227. GetSize = FormatNumber((Size / 1024) / 1024, 2) & "MB"
  228. End If
  229. End Function
  230. Private Function Bytes2Str(ByVal byt)
  231. If LenB(byt) = 0 Then
  232. Bytes2Str = ""
  233. Exit Function
  234. End If
  235. Dim mystream, bstr
  236. Set mystream =server.createobject("ADODB.Stream")
  237. mystream.Type = 2
  238. mystream.Mode = 3
  239. mystream.Open
  240. mystream.WriteText byt
  241. mystream.Position = 0
  242. mystream.CharSet = vCharSet
  243. mystream.Position = 2
  244. bstr = mystream.ReadText()
  245. mystream.Close
  246. Set mystream = Nothing
  247. Bytes2Str = bstr
  248. End Function
  249. Private Function GetErr(ByVal Num)
  250. Select Case Num
  251. Case 0
  252. GetErr = "COMPLETE"
  253. Case 1
  254. GetErr = "ERROR_FILE_EXCEEDS_MAXSIZE_LIMIT"
  255. Case 2
  256. GetErr = "ERROR_INVALID_ENCTYPEOR_METHOD"
  257. Case 3
  258. GetErr = "ERROR_INVALID_FILETYPE(." & ucase(vErrExe) & ")"
  259. Case 5
  260. GetErr = "ERROR_FILE_EXCEEDS_SIZE_LIMIT"
  261. End Select
  262. End Function
  263. Private Function Getname()
  264. Dim y, m, d, h, mm, S, r
  265. Randomize
  266. y = Year(Now)
  267. m = right("0" & Month(Now),2)
  268. d = right("0" & Day(Now),2)
  269. h = right("0" & Hour(Now),2)
  270. mm =right("0" & Minute(Now),2)
  271. S = right("0" & Second(Now),2)
  272. r = CInt(Rnd() * 10000)
  273. r = right("0000" & r,4)
  274. Getname = y & m & d & h & mm & S & r
  275. End Function
  276. Private Function checkEntryType()
  277. Dim ContentType, ctArray, bArray,RequestMethod
  278. RequestMethod=trim(LCase(Request.ServerVariables("REQUEST_METHOD")))
  279. if RequestMethod="" or RequestMethod<>"post" then
  280. checkEntryType = False
  281. exit function
  282. end if
  283. ContentType = LCase(Request.ServerVariables("HTTP_CONTENT_TYPE"))
  284. ctArray = Split(ContentType, ";")
  285. if ubound(ctarray)>=0 then
  286. If Trim(ctArray(0)) = "multipart/form-data" Then
  287. checkEntryType = True
  288. vboundary = Split(ContentType,"boundary=")(1)
  289. Else
  290. checkEntryType = False
  291. End If
  292. else
  293. checkEntryType = False
  294. end if
  295. End Function
  296. Public Function Forms(ByVal formname)
  297. If trim(formname) = "-1" Then
  298. Set Forms = Form
  299. Else
  300. If Form.Exists(LCase(formname)) Then
  301. Forms = Form(LCase(formname))
  302. Else
  303. Forms = ""
  304. End If
  305. End If
  306. End Function
  307. Public Function Files(ByVal formname)
  308. If trim(formname) = "-1" Then
  309. Set Files = Fils
  310. Else
  311. dim vname
  312. vname = LCase(formname) & vMuti & "0"
  313. if instr(formname,vMuti)>0 then vname = formname
  314. If Fils.Exists(vname) Then
  315. Set Files = Fils(vname)
  316. Else
  317. Set Files = New UploadFileEmpty
  318. End If
  319. End If
  320. End Function
  321. Public Function Files_Muti(ByVal formname,byval index)
  322. If trim(formname) = "-1" Then
  323. Set Files_Muti = Fils
  324. Else
  325. If Fils.Exists(LCase(formname) & vMuti & index) Then
  326. Set Files_Muti = Fils(LCase(formname) & vMuti & index)
  327. Else
  328. Set Files_Muti = New UploadFileEmpty
  329. End If
  330. End If
  331. End Function
  332. Public Function QuickSave(ByVal formname,Byval SavePath)
  333. Dim v, formStart,File,Result,SucceedCount
  334. SucceedCount = 0
  335. Dim TempFormName
  336. TempFormName = formname & vMuti
  337. For Each v In Fils
  338. If lcase(left(v,len(TempFormName))) = lcase(TempFormName) Then
  339. Set File = Fils(v)
  340. Result = File.saveToFile(SavePath,0,True)
  341. If Result Then SucceedCount = SucceedCount + 1
  342. 'Set File=Nothing
  343. End If
  344. Next
  345. QuickSave = SucceedCount
  346. End Function
  347. Private Function GetNextFormName(byval formname)
  348. Dim formStart,currentIndex
  349. formStart = left(formname,instr(formname,vMuti)+len(vMuti)-1)
  350. currentIndex = mid(formname,instr(formname,vMuti)+len(vMuti))
  351. currentIndex =cint(currentIndex)
  352. do while Fils.Exists(formname)
  353. currentIndex = currentIndex + 1
  354. formname = formStart & currentIndex
  355. loop
  356. GetNextFormName = formname
  357. End Function
  358. End Class
  359. Class UploadFileEmpty
  360. Public Property Get IsFile()
  361. IsFile = false
  362. End Property
  363. End Class
  364. Class UploadFileEx
  365. Private mvarFormName , mvarNewName , mvarLocalName , mvarFileName , mvarUserSetName , mvarContentType ,mException,mvarPosition
  366. Private mvarSize , mvarValue , mvarPath , mvarExtend
  367. Public Property Let Extend(ByVal vData )
  368. mvarExtend = vData
  369. End Property
  370. Public Property Get Extend()
  371. Extend = mvarExtend
  372. End Property
  373. Public Property Get IsFile()
  374. IsFile = true
  375. End Property
  376. Public Property Let Path(ByVal vData )
  377. mvarPath = vData
  378. End Property
  379. Public Property Get Path()
  380. Path = mvarPath
  381. End Property
  382. Public Property Get Exception()
  383. Exception = mException
  384. End Property
  385. Public Property Let Value(ByVal vData )
  386. mvarValue = vData
  387. End Property
  388. Public Property Get Value()
  389. Value = mvarValue
  390. End Property
  391. Public Property Let Size(ByVal vData )
  392. mvarSize = vData
  393. End Property
  394. Public Property Get Size()
  395. Size = mvarSize
  396. End Property
  397. Public Property Let Position(ByVal vData )
  398. mvarPosition = vData
  399. End Property
  400. Public Property Get Position()
  401. Size = mvarPosition
  402. End Property
  403. Public Property Let ContentType(ByVal vData )
  404. mvarContentType = vData
  405. End Property
  406. Public Property Get ContentType()
  407. ContentType = mvarContentType
  408. End Property
  409. Public Property Let UserSetName(ByVal vData )
  410. mvarUserSetName = vData
  411. End Property
  412. Public Property Get UserSetName()
  413. UserSetName = mvarUserSetName
  414. End Property
  415. Public Property Let FileName(ByVal vData )
  416. mvarFileName = vData
  417. End Property
  418. Public Property Get FileName()
  419. FileName = mvarFileName
  420. End Property
  421. Public Property Let LocalName(ByVal vData )
  422. mvarLocalName = vData
  423. End Property
  424. Public Property Get LocalName()
  425. LocalName = mvarLocalName
  426. End Property
  427. Public Property Let NewName(ByVal vData )
  428. mvarNewName = vData
  429. End Property
  430. Public Property Get NewName()
  431. NewName = mvarNewName
  432. End Property
  433. Public Property Let FormName(ByVal vData )
  434. mvarFormName = vData
  435. End Property
  436. Public Property Get FormName()
  437. FormName = mvarFormName
  438. End Property
  439. Private Sub Class_Initialize()
  440. mvarSize =0
  441. mvarFormName = ""
  442. End Sub
  443. Public Function SaveToFile(ByVal Path , byval tOption, byval OverWrite)
  444. On Error Resume Next
  445. Dim IsP
  446. IsP = (InStr(Path, ":") = 2)
  447. If Not IsP Then Path = Server.MapPath(Path)
  448. Path = Replace(Path, "/", "\")
  449. If Mid(Path, Len(Path) - 1) <> "\" Then Path = Path + "\"
  450. CreateFolder Path
  451. mvarPath = Path
  452. If tOption = 1 Then
  453. Path = Path & mvarLocalName: mvarFileName = mvarLocalName
  454. Else
  455. If tOption = -1 And mvarUserSetName <> "" Then
  456. Path = Path & mvarUserSetName & "." & mvarExtend: mvarFileName = mvarUserSetName & "." & mvarExtend
  457. Else
  458. Path = Path & mvarNewName: mvarFileName = mvarNewName
  459. End If
  460. End If
  461. If Not OverWrite Then
  462. Path = GetFilePath()
  463. End If
  464. Dim tmpStrm
  465. Set tmpStrm =server.CreateObject("ADODB.Stream")
  466. tmpStrm.Mode = 3
  467. tmpStrm.Type = 1
  468. tmpStrm.Open
  469. StreamT.Position = mvarPosition
  470. StreamT.copyto tmpStrm,mvarSize
  471. tmpStrm.SaveToFile Path, 2
  472. tmpStrm.Close
  473. Set tmpStrm = Nothing
  474. 'Set SaveToFile = new ErrorMessage_
  475. If Not Err Then
  476. SaveToFile = true
  477. Else
  478. SaveToFile = false
  479. mException=Err.Description
  480. End If
  481. End Function
  482. Public Function GetBytes()
  483. StreamT.Position = mvarPosition
  484. GetBytes = StreamT.read(mvarSize)
  485. End Function
  486. Private Function CreateFolder(ByVal folderPath )
  487. Dim oFSO
  488. Set oFSO = server.CreateObject("Scripting.FileSystemObject")
  489. Dim sParent
  490. sParent = oFSO.GetParentFolderName(folderPath)
  491. If sParent = "" Then Exit Function
  492. If Not oFSO.FolderExists(sParent) Then CreateFolder (sParent)
  493. If Not oFSO.FolderExists(folderPath) Then oFSO.CreateFolder (folderPath)
  494. Set oFSO = Nothing
  495. End Function
  496. Private Function GetFilePath()
  497. Dim oFSO, Fname , FNameL , i
  498. i = 0
  499. Set oFSO = server.CreateObject("Scripting.FileSystemObject")
  500. Fname = mvarPath & mvarFileName
  501. FNameL = Mid(mvarFileName, 1, InStr(mvarFileName, ".") - 1)
  502. Do While oFSO.FileExists(Fname)
  503. Fname = mvarPath & FNameL & "(" & i & ")." & mvarExtend
  504. mvarFileName = FNameL & "(" & i & ")." & mvarExtend
  505. i = i + 1
  506. Loop
  507. Set oFSO = Nothing
  508. GetFilePath = Fname
  509. End Function
  510. End Class
  511. %>