UpLoad_Class.asp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. <%
  2. '=========================================================
  3. '类名: AnUpLoad(艾恩无组件上传类)
  4. '作者: Anlige
  5. '版本: 艾恩ASP无组件上传类V11.03.25
  6. '开发日期: 2008-4-12
  7. '修改日期: 2011-03025
  8. '主页: 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, pID, vOP, vErrExe,vboundary, vLostTime, vMode, vFileCount
  16. '==============================
  17. '设置和读取属性开始
  18. '==============================
  19. Public Property Let Mode(ByVal value)
  20. vMode = value
  21. End Property
  22. Public Property Let MaxSize(ByVal value)
  23. vMaxSize = value
  24. End Property
  25. Public Property Let SingleSize(ByVal value)
  26. vSingleSize = value
  27. End Property
  28. Public Property Let Exe(ByVal value)
  29. vExe = LCase(value)
  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 ProcessID()
  50. ProcessID = pID
  51. End Property
  52. Public Property Let openProcesser(ByVal value)
  53. vOP = value
  54. End Property
  55. Public Property Get LostTime()
  56. LostTime = vLostTime
  57. End Property
  58. '==============================
  59. '设置和读取属性结束,初始化类
  60. '==============================
  61. Private Sub Class_Initialize()
  62. set Form = server.createobject("Scripting.Dictionary")
  63. set Fils = server.createobject("Scripting.Dictionary")
  64. Set StreamT = server.CreateObject("Adodb.stream")
  65. vVersion = "艾恩ASP无组件上传类V10.10.22"
  66. vMaxSize = -1
  67. vSingleSize = -1
  68. vErr = -1
  69. vExe = ""
  70. vTotalSize = 0
  71. vCharSet = "utf-8"
  72. vOP=false
  73. pID="AnUpload"
  74. setApp "",0,0,""
  75. vMode = 0
  76. End Sub
  77. Private Sub Class_Terminate()
  78. Dim f
  79. Form.RemoveAll()
  80. For each f in Fils
  81. Fils(f).value=empty
  82. Set Fils(f) = Nothing
  83. Next
  84. Fils.RemoveAll()
  85. Set Form = Nothing
  86. Set Fils = Nothing
  87. StreamT.Close()
  88. Set StreamT = Nothing
  89. End Sub
  90. '==============================
  91. '函数名:GetData
  92. '作用:处理客户端提交来的所有数据
  93. '==============================
  94. Public Sub GetData()
  95. Dim time1
  96. time1 = timer()
  97. if vOP And trim(request.querystring("processid"))<>"" then pID=request.querystring("processid")
  98. Dim value, str, bcrlf, fpos, sSplit, slen, istart,ef
  99. Dim TotalBytes,tempdata,BytesRead,ChunkReadSize,PartSize,DataPart,formend, formhead, startpos, endpos, formname, FileName, fileExe, valueend, NewName,localname,type_1,contentType
  100. TotalBytes = Request.TotalBytes
  101. ef = false
  102. If checkEntryType = false Then ef = true : vErr = 2
  103. '下面3句注释掉了,因为在IIS5.0中,如果上传大小大于限制大小的文件,会出错,一直没找到解决方法。如果是在IIS5以上的版本使用,可以取消下面3句的注释
  104. 'If Not ef Then
  105. 'If vMaxSize > 0 And TotalBytes > vMaxSize Then ef = true : vErr = 1
  106. 'End If
  107. If ef Then Exit Sub
  108. If vMode = 0 Then
  109. vTotalSize = 0
  110. StreamT.Type = 1
  111. StreamT.Mode = 3
  112. StreamT.Open
  113. BytesRead = 0
  114. ChunkReadSize = 1024 * 16
  115. Do While BytesRead < TotalBytes
  116. PartSize = ChunkReadSize
  117. If PartSize + BytesRead > TotalBytes Then PartSize = TotalBytes - BytesRead
  118. DataPart = Request.BinaryRead(PartSize)
  119. StreamT.Write DataPart
  120. BytesRead = BytesRead + PartSize
  121. setApp "uploading",TotalBytes,BytesRead,""
  122. Loop
  123. setApp "uploaded",TotalBytes,BytesRead,""
  124. StreamT.Position = 0
  125. tempdata = StreamT.Read
  126. Else
  127. tempdata = Request.BinaryRead(TotalBytes)
  128. End If
  129. bcrlf = ChrB(13) & ChrB(10)
  130. fpos = InStrB(1, tempdata, bcrlf)
  131. sSplit = MidB(tempdata, 1, fpos - 1)
  132. slen = LenB(sSplit)
  133. istart = slen + 2
  134. Do
  135. formend = InStrB(istart, tempdata, bcrlf & bcrlf)
  136. formhead = MidB(tempdata, istart, formend - istart)
  137. str = Bytes2Str(formhead)
  138. startpos = InStr(str, "name=""") + 6
  139. endpos = InStr(startpos, str, """")
  140. formname = LCase(Mid(str, startpos, endpos - startpos))
  141. valueend = InStrB(formend + 3, tempdata, sSplit)
  142. If InStr(str, "filename=""") > 0 Then
  143. startpos = InStr(str, "filename=""") + 10
  144. endpos = InStr(startpos, str, """")
  145. type_1=instr(endpos,lcase(str),"content-type")
  146. contentType=trim(mid(str,type_1+13))
  147. FileName = Mid(str, startpos, endpos - startpos)
  148. If Trim(FileName) <> "" Then
  149. LocalName = FileName
  150. FileName = Replace(FileName, "/", "\")
  151. FileName = Mid(FileName, InStrRev(FileName, "\") + 1)
  152. If instr(FileName,".")>0 Then
  153. fileExe = Split(FileName, ".")(UBound(Split(FileName, ".")))
  154. else
  155. fileExe = ""
  156. End If
  157. If vExe <> "" Then '判断扩展名
  158. If checkExe(fileExe) = True Then
  159. vErr = 3
  160. vErrExe = fileExe
  161. tempdata = empty
  162. Exit Sub
  163. End If
  164. End If
  165. NewName = Getname()
  166. NewName = NewName & "." & fileExe
  167. vTotalSize = vTotalSize + valueend - formend - 6
  168. If vSingleSize > 0 And (valueend - formend - 6) > vSingleSize Then '判断上传单个文件大小
  169. vErr = 5
  170. tempdata = empty
  171. Exit Sub
  172. End If
  173. If vMaxSize > 0 And vTotalSize > vMaxSize Then '判断上传数据总大小
  174. vErr = 1
  175. tempdata = empty
  176. Exit Sub
  177. End If
  178. If Fils.Exists(formname) Then
  179. vErr = 4
  180. tempdata = empty
  181. Exit Sub
  182. Else
  183. Dim fileCls:set fileCls= new UploadFileEx
  184. fileCls.ContentType=contentType
  185. fileCls.Size = (valueend - formend - 6)
  186. fileCls.Position = (formend + 3)
  187. fileCls.FormName = formname
  188. fileCls.NewName = NewName
  189. fileCls.FileName = FileName
  190. fileCls.LocalName = FileName
  191. fileCls.extend=split(NewName,".")(ubound(split(NewName,".")))
  192. Fils.Add formname, fileCls
  193. Set fileCls = Nothing
  194. End If
  195. End If
  196. Else
  197. value = MidB(tempdata, formend + 4, valueend - formend - 6)
  198. If Form.Exists(formname) Then
  199. Form(formname) = Form(formname) & "," & Bytes2Str(value)
  200. Else
  201. Form.Add formname, Bytes2Str(value)
  202. End If
  203. End If
  204. istart = valueend + 2 + slen
  205. Loop Until (istart + 2) >= LenB(tempdata)
  206. vErr = 0
  207. tempdata = empty
  208. vLostTime = FormatNumber((timer-time1)*1000,2)
  209. End Sub
  210. Public sub setApp(stp,total,current,desc)
  211. Application.lock()
  212. Application(pID)="{ID:""" & pID & """,step:""" & stp & """,total:" & total & ",now:" & current & ",description:""" & desc & """,dt:""" & now() & """}"
  213. Application.unlock()
  214. end sub
  215. '==============================
  216. '判断扩展名
  217. '==============================
  218. Private Function checkExe(ByVal ex)
  219. Dim notIn: notIn = True
  220. If vExe="*" then
  221. notIn=false
  222. elseIf InStr(1, vExe, "|") > 0 Then
  223. Dim tempExe: tempExe = Split(vExe, "|")
  224. Dim I: I = 0
  225. For I = 0 To UBound(tempExe)
  226. If LCase(ex) = tempExe(I) Then
  227. notIn = False
  228. Exit For
  229. End If
  230. Next
  231. Else
  232. If vExe = LCase(ex) Then
  233. notIn = False
  234. End If
  235. End If
  236. checkExe = notIn
  237. End Function
  238. '==============================
  239. '把数字转换为文件大小显示方式
  240. '==============================
  241. Public Function GetSize(ByVal Size)
  242. If Size < 1024 Then
  243. GetSize = FormatNumber(Size, 2) & "B"
  244. ElseIf Size >= 1024 And Size < 1048576 Then
  245. GetSize = FormatNumber(Size / 1024, 2) & "KB"
  246. ElseIf Size >= 1048576 Then
  247. GetSize = FormatNumber((Size / 1024) / 1024, 2) & "MB"
  248. End If
  249. End Function
  250. '==============================
  251. '二进制数据转换为字符
  252. '==============================
  253. Private Function Bytes2Str(ByVal byt)
  254. If LenB(byt) = 0 Then
  255. Bytes2Str = ""
  256. Exit Function
  257. End If
  258. Dim mystream, bstr
  259. Set mystream =server.createobject("ADODB.Stream")
  260. mystream.Type = 2
  261. mystream.Mode = 3
  262. mystream.Open
  263. mystream.WriteText byt
  264. mystream.Position = 0
  265. mystream.CharSet = vCharSet
  266. mystream.Position = 2
  267. bstr = mystream.ReadText()
  268. mystream.Close
  269. Set mystream = Nothing
  270. Bytes2Str = bstr
  271. End Function
  272. '==============================
  273. '获取错误描述
  274. '==============================
  275. Private Function GetErr(ByVal Num)
  276. Select Case Num
  277. Case 0
  278. GetErr = "数据处理完毕!"
  279. Case 1
  280. GetErr = "上传数据超过" & GetSize(vMaxSize) & "限制!可设置MaxSize属性来改变限制!"
  281. Case 2
  282. GetErr = "未设置上传表单enctype属性为multipart/form-data或者未设置method属性为Post,上传无效!"
  283. Case 3
  284. GetErr = "含有非法扩展名(" & vErrExe & ")文件!只能上传扩展名为" & Replace(vExe, "|", ",") & "的文件"
  285. Case 4
  286. GetErr = "对不起,程序不允许使用相同name属性的文件域!"
  287. Case 5
  288. GetErr = "单个文件大小超出" & GetSize(vSingleSize) & "的上传限制!"
  289. End Select
  290. End Function
  291. '==============================
  292. '根据日期生成随机文件名
  293. '==============================
  294. Private Function Getname()
  295. Dim y, m, d, h, mm, S, r
  296. Randomize
  297. y = Year(Now)
  298. m = right("0" & Month(Now),2)
  299. d = right("0" & Day(Now),2)
  300. h = right("0" & Hour(Now),2)
  301. mm =right("0" & Minute(Now),2)
  302. S = right("0" & Second(Now),2)
  303. r = 0
  304. r = CInt(Rnd() * 10000)
  305. S = right("0000" & r,4)
  306. Getname = y & m & d & h & mm & S & r
  307. End Function
  308. '==============================
  309. '检测上传类型是否为multipart/form-data
  310. '==============================
  311. Private Function checkEntryType()
  312. Dim ContentType, ctArray, bArray,RequestMethod
  313. RequestMethod=trim(LCase(Request.ServerVariables("REQUEST_METHOD")))
  314. if RequestMethod="" or RequestMethod<>"post" then
  315. checkEntryType = False
  316. exit function
  317. end if
  318. ContentType = LCase(Request.ServerVariables("HTTP_CONTENT_TYPE"))
  319. ctArray = Split(ContentType, ";")
  320. if ubound(ctarray)>=0 then
  321. If Trim(ctArray(0)) = "multipart/form-data" Then
  322. checkEntryType = True
  323. vboundary = Split(ContentType,"boundary=")(1)
  324. Else
  325. checkEntryType = False
  326. End If
  327. else
  328. checkEntryType = False
  329. end if
  330. End Function
  331. '==============================
  332. '获取上传表单值,参数可选,如果为-1则返回一个包含所有表单项的一个dictionary对象
  333. '==============================
  334. Public Function Forms(ByVal formname)
  335. If trim(formname) = "-1" Then
  336. Set Forms = Form
  337. Else
  338. If Form.Exists(LCase(formname)) Then
  339. Forms = Form(LCase(formname))
  340. Else
  341. Forms = ""
  342. End If
  343. End If
  344. End Function
  345. '==============================
  346. '获取上传的文件类,参数可选,如果为-1则返回一个包含所有上传文件类的一个dictionary对象
  347. '==============================
  348. Public Function Files(ByVal formname)
  349. If trim(formname) = "-1" Then
  350. Set Files = Fils
  351. Else
  352. If Fils.Exists(LCase(formname)) Then
  353. Set Files = Fils(LCase(formname))
  354. Else
  355. Set Files = Nothing
  356. End If
  357. End If
  358. End Function
  359. End Class
  360. Class UploadFileEx
  361. Private mvarFormName , mvarNewName , mvarLocalName , mvarFileName , mvarUserSetName , mvarContentType ,mException,mvarPosition
  362. Private mvarSize , mvarValue , mvarPath , mvarExtend ,mvarWidth, mvarHeight
  363. Public Property Let Extend(ByVal vData )
  364. mvarExtend = vData
  365. End Property
  366. Public Property Get Extend()
  367. Extend = mvarExtend
  368. End Property
  369. Public Property Get Width()
  370. Width = mvarWidth
  371. End Property
  372. Public Property Get Height()
  373. Height = mvarHeight
  374. End Property
  375. Public Property Let Path(ByVal vData )
  376. mvarPath = vData
  377. End Property
  378. Public Property Get Path()
  379. Path = mvarPath
  380. End Property
  381. Public Property Get Exception()
  382. Exception = mException
  383. End Property
  384. Public Property Let Value(ByVal vData )
  385. mvarValue = vData
  386. End Property
  387. Public Property Get Value()
  388. Value = mvarValue
  389. End Property
  390. Public Property Let Size(ByVal vData )
  391. mvarSize = vData
  392. End Property
  393. Public Property Get Size()
  394. Size = mvarSize
  395. End Property
  396. Public Property Let Position(ByVal vData )
  397. mvarPosition = vData
  398. End Property
  399. Public Property Get Position()
  400. Size = mvarPosition
  401. End Property
  402. Public Property Let ContentType(ByVal vData )
  403. mvarContentType = vData
  404. End Property
  405. Public Property Get ContentType()
  406. ContentType = mvarContentType
  407. End Property
  408. Public Property Let UserSetName(ByVal vData )
  409. mvarUserSetName = vData
  410. End Property
  411. Public Property Get UserSetName()
  412. UserSetName = mvarUserSetName
  413. End Property
  414. Public Property Let FileName(ByVal vData )
  415. mvarFileName = vData
  416. End Property
  417. Public Property Get FileName()
  418. FileName = mvarFileName
  419. End Property
  420. Public Property Let LocalName(ByVal vData )
  421. mvarLocalName = vData
  422. End Property
  423. Public Property Get LocalName()
  424. LocalName = mvarLocalName
  425. End Property
  426. Public Property Let NewName(ByVal vData )
  427. mvarNewName = vData
  428. End Property
  429. Public Property Get NewName()
  430. NewName = mvarNewName
  431. End Property
  432. Public Property Let FormName(ByVal vData )
  433. mvarFormName = vData
  434. End Property
  435. Public Property Get FormName()
  436. FormName = mvarFormName
  437. End Property
  438. Private Sub Class_Initialize()
  439. mvarSize =0
  440. mvarWidth = 0
  441. mvarHeight = 0
  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. If Not Err Then
  475. Set SaveToFile = objFromJson("{error:false}")
  476. Else
  477. Set SaveToFile = objFromJson("{error:true,description:'" & replace(Err.Description,"'","\'") & "'}")
  478. mException=Err.Description
  479. End If
  480. End Function
  481. Public Function GetBytes()
  482. StreamT.Position = mvarPosition
  483. GetBytes = StreamT.read(mvarSize)
  484. End Function
  485. Private Function CreateFolder(ByVal folderPath )
  486. Dim oFSO
  487. Set oFSO = server.CreateObject("Scripting.FileSystemObject")
  488. Dim sParent
  489. sParent = oFSO.GetParentFolderName(folderPath)
  490. If sParent = "" Then Exit Function
  491. If Not oFSO.FolderExists(sParent) Then CreateFolder (sParent)
  492. If Not oFSO.FolderExists(folderPath) Then oFSO.CreateFolder (folderPath)
  493. Set oFSO = Nothing
  494. End Function
  495. Private Function GetFilePath()
  496. Dim oFSO, Fname , FNameL , i
  497. i = 0
  498. Set oFSO = server.CreateObject("Scripting.FileSystemObject")
  499. Fname = mvarPath & mvarFileName
  500. FNameL = Mid(mvarFileName, 1, InStr(mvarFileName, ".") - 1)
  501. Do While oFSO.FileExists(Fname)
  502. Fname = mvarPath & FNameL & "(" & i & ")." & mvarExtend
  503. mvarFileName = FNameL & "(" & i & ")." & mvarExtend
  504. i = i + 1
  505. Loop
  506. Set oFSO = Nothing
  507. GetFilePath = Fname
  508. End Function
  509. End Class
  510. %>
  511. <script language="jscript" runat="server">
  512. function objFromJson(str){
  513. eval("var _temp=(" + str + ");");
  514. return _temp;
  515. }
  516. </script>