请选择 进入手机版 | 继续访问电脑版
查看: 372|回复: 0

[.NET开发] asp 无组件上传类

3万

主题

3万

帖子

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
100197
发表于 2015-11-23 17:52:14
无组件上传,必不可少的代码
  1. Dim StreamT
  2. Class AnUpLoad
  3. Private Form, Fils
  4. Private vCharSet, vMaxSize, vSingleSize, vErr, vVersion, vTotalSize, vExe, pID,vOP
  5. Public Property Let MaxSize(ByVal value)
  6. vMaxSize = value
  7. End Property
  8. Public Property Let SingleSize(ByVal value)
  9. vSingleSize = value
  10. End Property
  11. Public Property Let Exe(ByVal value)
  12. vExe = LCase(value)
  13. End Property
  14. Public Property Let CharSet(ByVal value)
  15. vCharSet = value
  16. End Property
  17. Public Property Get ErrorID()
  18. ErrorID = vErr
  19. End Property
  20. Public Property Get Description()
  21. Description = GetErr(vErr)
  22. End Property
  23. Public Property Get Version()
  24. Version = vVersion
  25. End Property
  26. Public Property Get TotalSize()
  27. TotalSize = vTotalSize
  28. End Property
  29. Public Property Get ProcessID()
  30. ProcessID = pID
  31. End Property
  32. Public Property Let openProcesser(ByVal value)
  33. vOP = value
  34. End Property
  35. Private Sub Class_Initialize()
  36. set StreamT=server.createobject("ADODB.STREAM")
  37. set Form = server.createobject("Scripting.Dictionary")
  38. set Fils = server.createobject("Scripting.Dictionary")
  39. vVersion = "V9.9.9"
  40. vMaxSize = -1
  41. vSingleSize = -1
  42. vErr = -1
  43. vExe = ""
  44. vTotalSize = 0
  45. vCharSet = "utf-8"
  46. vOP=false
  47. pID="Upload"
  48. setApp "",0,0,""
  49. End Sub
  50. Private Sub Class_Terminate()
  51. Form.RemoveAll()
  52. Fils.RemoveAll()
  53. Set Form = Nothing
  54. Set Fils = Nothing
  55. Set StreamT = Nothing
  56. End Sub
  57. Public Sub GetData()
  58. If vMaxSize > 0 And Request.TotalBytes > vMaxSize Then
  59. vErr = 1
  60. Exit Sub
  61. End If
  62. if vOP then pID=request.querystring("processid")
  63. Dim value, str, bcrlf, fpos, sSplit, slen, istart
  64. Dim TotalBytes,BytesRead,ChunkReadSize,PartSize,DataPart,tempdata,formend, formhead, startpos, endpos, formname, FileName, fileExe, valueend, NewName,localname,type_1,contentType
  65. If checkEntryType = True Then
  66. vTotalSize = 0
  67. StreamT.Type = 1
  68. StreamT.Mode = 3
  69. StreamT.Open
  70. TotalBytes = Request.TotalBytes
  71. BytesRead = 0
  72. ChunkReadSize = 1024 * 36
  73. Do While BytesRead < TotalBytes
  74. PartSize = ChunkReadSize
  75. If PartSize + BytesRead > TotalBytes Then PartSize = TotalBytes - BytesRead
  76. DataPart = Request.BinaryRead(PartSize)
  77. StreamT.Write DataPart
  78. BytesRead = BytesRead + PartSize
  79. setApp "uploading",TotalBytes,BytesRead,""
  80. Loop
  81. setApp "uploaded",TotalBytes,BytesRead,""
  82. StreamT.Position = 0
  83. tempdata = StreamT.Read
  84. bcrlf = ChrB(13) & ChrB(10)
  85. fpos = InStrB(1, tempdata, bcrlf)
  86. sSplit = MidB(tempdata, 1, fpos - 1)
  87. slen = LenB(sSplit)
  88. istart = slen + 2
  89. Do
  90. formend = InStrB(istart, tempdata, bcrlf & bcrlf)
  91. formhead = MidB(tempdata, istart, formend - istart)
  92. str = Bytes2Str(formhead)
  93. startpos = InStr(str, "name=""") + 6
  94. endpos = InStr(startpos, str, """")
  95. formname = LCase(Mid(str, startpos, endpos - startpos))
  96. valueend = InStrB(formend + 3, tempdata, sSplit)
  97. If InStr(str, "filename=""") > 0 Then
  98. startpos = InStr(str, "filename=""") + 10
  99. endpos = InStr(startpos, str, """")
  100. type_1=instr(endpos,lcase(str),"content-type")
  101. contentType=trim(mid(str,type_1+13))
  102. FileName = Mid(str, startpos, endpos - startpos)
  103. If Trim(FileName) <> "" Then
  104. LocalName = FileName
  105. FileName = Replace(FileName, "/", "")
  106. FileName = Mid(FileName, InStrRev(FileName, "") + 1)
  107. setApp "processing",TotalBytes,BytesRead,FileName
  108. If instr(FileName,".")>0 Then
  109. fileExe = Split(FileName, ".")(UBound(Split(FileName, ".")))
  110. else
  111. fileExe = ""
  112. End If
  113. If vExe <> "" Then
  114. If checkExe(fileExe) = True Then
  115. vErr = 3
  116. Exit Sub
  117. End If
  118. End If
  119. NewName = Getname()
  120. NewName = NewName & "." & fileExe
  121. vTotalSize = vTotalSize + valueend - formend - 6
  122. If vSingleSize > 0 And (valueend - formend - 6) > vSingleSize Then
  123. vErr = 5
  124. Exit Sub
  125. End If
  126. If vMaxSize > 0 And vTotalSize > vMaxSize Then
  127. vErr = 1
  128. Exit Sub
  129. End If
  130. If Fils.Exists(formname) Then
  131. vErr = 4
  132. Exit Sub
  133. Else
  134. Dim fileCls:set fileCls=New fileAction
  135. fileCls.ContentType=contentType
  136. fileCls.Size = (valueend - formend - 6)
  137. fileCls.Position = (formend + 3)
  138. fileCls.NewName = NewName
  139. fileCls.LocalName = FileName
  140. Fils.Add formname, fileCls
  141. Form.Add formname, LocalName
  142. Set fileCls = Nothing
  143. End If
  144. End If
  145. Else
  146. value = MidB(tempdata, formend + 4, valueend - formend - 6)
  147. If Form.Exists(formname) Then
  148. Form(formname) = Form(formname) & "," & Bytes2Str(value)
  149. Else
  150. Form.Add formname, Bytes2Str(value)
  151. End If
  152. End If
  153. istart = valueend + 2 + slen
  154. Loop Until (istart + 2) >= LenB(tempdata)
  155. vErr = 0
  156. Else
  157. vErr = 2
  158. End If
  159. setApp "processed",TotalBytes,BytesRead,""
  160. if err then setApp "faild",1,0,err.description
  161. End Sub
  162. Public sub setApp(stp,total,current,desc)
  163. Application.lock()
  164. Application(pID)="{ID:""" & pID & """,step:""" & stp & """,total:" & total & ",now:" & current & ",description:""" & desc & """,dt:""" & now() & """}"
  165. Application.unlock()
  166. end sub
  167. Private Function checkExe(ByVal ex)
  168. Dim notIn: notIn = True
  169. If vExe="*" then
  170. notIn=false
  171. elseIf InStr(1, vExe, "|") > 0 Then
  172. Dim tempExe: tempExe = Split(vExe, "|")
  173. Dim I: I = 0
  174. For I = 0 To UBound(tempExe)
  175. If LCase(ex) = tempExe(I) Then
  176. notIn = False
  177. Exit For
  178. End If
  179. Next
  180. Else
  181. If vExe = LCase(ex) Then
  182. notIn = False
  183. End If
  184. End If
  185. checkExe = notIn
  186. End Function
  187. Public Function GetSize(ByVal Size)
  188. If Size < 1024 Then
  189. GetSize = FormatNumber(Size, 2) & "B"
  190. ElseIf Size >= 1024 And Size < 1048576 Then
  191. GetSize = FormatNumber(Size / 1024, 2) & "KB"
  192. ElseIf Size >= 1048576 Then
  193. GetSize = FormatNumber((Size / 1024) / 1024, 2) & "MB"
  194. End If
  195. End Function
  196. Private Function Bytes2Str(ByVal byt)
  197. If LenB(byt) = 0 Then
  198. Bytes2Str = ""
  199. Exit Function
  200. End If
  201. Dim mystream, bstr
  202. Set mystream =server.createobject("ADODB.Stream")
  203. mystream.Type = 2
  204. mystream.Mode = 3
  205. mystream.Open
  206. mystream.WriteText byt
  207. mystream.Position = 0
  208. mystream.CharSet = vCharSet
  209. mystream.Position = 2
  210. bstr = mystream.ReadText()
  211. mystream.Close
  212. Set mystream = Nothing
  213. Bytes2Str = bstr
  214. End Function
  215. Private Function GetErr(ByVal Num)
  216. Select Case Num
  217. Case 0
  218. GetErr = "数据处理完毕!"
  219. Case 1
  220. GetErr = "上传数据超过" & GetSize(vMaxSize) & "限制!可设置MaxSize属性来改变限制!"
  221. Case 2
  222. GetErr = "未设置上传表单enctype属性为multipart/form-data或者未设置method属性为Post,上传无效!"
  223. Case 3
  224. GetErr = "含有非法扩展名文件!只能上传扩展名为" & Replace(vExe, "|", ",") & "的文件"
  225. Case 4
  226. GetErr = "对不起,程序不允许使用相同name属性的文件域!"
  227. Case 5
  228. GetErr = "单个文件大小超出" & GetSize(vSingleSize) & "的上传限制!"
  229. End Select
  230. End Function
  231. Private Function Getname()
  232. Dim y, m, d, h, mm, S, r
  233. Randomize
  234. y = Year(Now)
  235. m = Month(Now): If m < 10 Then m = "0" & m
  236. d = Day(Now): If d < 10 Then d = "0" & d
  237. h = Hour(Now): If h < 10 Then h = "0" & h
  238. mm = Minute(Now): If mm < 10 Then mm = "0" & mm
  239. S = Second(Now): If S < 10 Then S = "0" & S
  240. r = 0
  241. r = CInt(Rnd() * 1000)
  242. If r < 10 Then r = "00" & r
  243. If r < 100 And r >= 10 Then r = "0" & r
  244. Getname = y & m & d & h & mm & S & r
  245. End Function
  246. Private Function checkEntryType()
  247. Dim ContentType, ctArray, bArray,RequestMethod
  248. RequestMethod=trim(LCase(Request.ServerVariables("REQUEST_METHOD")))
  249. if RequestMethod="" or RequestMethod<>"post" then
  250. checkEntryType = False
  251. exit function
  252. end if
  253. ContentType = LCase(Request.ServerVariables("HTTP_CONTENT_TYPE"))
  254. ctArray = Split(ContentType, ";")
  255. if ubound(ctarray)>=0 then
  256. If Trim(ctArray(0)) = "multipart/form-data" Then
  257. checkEntryType = True
  258. Else
  259. checkEntryType = False
  260. End If
  261. else
  262. checkEntryType = False
  263. end if
  264. End Function
  265. Public Function Forms(ByVal formname)
  266. If trim(formname) = "-1" Then
  267. Set Forms = Form
  268. Else
  269. If Form.Exists(LCase(formname)) Then
  270. Forms = Form(LCase(formname))
  271. Else
  272. Forms = ""
  273. End If
  274. End If
  275. End Function
  276. Public Function Files(ByVal formname)
  277. If trim(formname) = "-1" Then
  278. Set Files = Fils
  279. Else
  280. If Fils.Exists(LCase(formname)) Then
  281. Set Files = Fils(LCase(formname))
  282. Else
  283. Set Files = Nothing
  284. End If
  285. End If
  286. End Function
  287. Public Function SaveAs(ByVal formname,ByVal path, ByVal saveType )
  288. dim vfileAction
  289. set vfileAction=Files(formname)
  290. if vfileAction.FileName<>"" then
  291. if vfileAction.SaveToFile(path,saveType) then
  292. SaveAs=vfileAction.FileName
  293. else
  294. SaveAs="Has Error!"
  295. end if
  296. end if
  297. set vfileAction=nothing
  298. end function
  299. End Class
  300. Class fileAction
  301. Private vSize, vPosition, vName, vNewName, vLocalName, vPath, saveName,vContentType
  302. Public Property Let NewName(ByVal value)
  303. vNewName = value
  304. End Property
  305. Public Property Get NewName()
  306. NewName = vNewName
  307. End Property
  308. Public Property Let ContentType(vData)
  309. vContentType = vData
  310. End Property
  311. Public Property Get ContentType()
  312. ContentType = vContentType
  313. End Property
  314. Public Property Let LocalName(ByVal value)
  315. vLocalName = value
  316. vName = value
  317. End Property
  318. Public Property Get LocalName()
  319. LocalName = vLocalName
  320. End Property
  321. Public Property Get FileName()
  322. FileName = vName
  323. End Property
  324. Public Property Let Position(ByVal value)
  325. vPosition = value
  326. End Property
  327. Public Property Let Size(ByVal value)
  328. vSize = value
  329. End Property
  330. Public Property Get Size()
  331. Size = vSize
  332. End Property
  333. Public Function SaveToFile(ByVal path, ByVal saveType)
  334. On Error Resume Next
  335. Err.Clear
  336. vPath = Replace(path, "/", "")
  337. If Right(vPath, 1) <> "" Then vPath = vPath & ""
  338. CreateFolder vPath
  339. Dim mystream
  340. Set mystream =server.createobject("ADODB.Stream")
  341. mystream.Type = 1
  342. mystream.Mode = 3
  343. mystream.Open
  344. StreamT.Position = vPosition
  345. StreamT.CopyTo mystream, vSize
  346. vName = vNewName
  347. If saveType = 1 Then vName = vLocalName
  348. mystream.SaveToFile vPath & vName, 2
  349. mystream.Close
  350. Set mystream = Nothing
  351. If Err Then
  352. SaveToFile = False
  353. Else
  354. SaveToFile = True
  355. End If
  356. End Function
  357. Public Function GetBytes()
  358. StreamT.Position = vPosition
  359. GetBytes = StreamT.Read(vSize)
  360. End Function
  361. Private Function CreateFolder(ByVal FolderPath)
  362. on error resume next
  363. Dim FolderArray
  364. Dim i
  365. Dim DiskName
  366. Dim Created
  367. Dim FSO : Set FSO = Server.CreateObject("Scripting.FileSystemObject")
  368. If FSO.FolderExists(FolderPath) Then
  369. Set Fso = Nothing
  370. Exit Function
  371. End If
  372. FolderPath = Replace(FolderPath, "/", "")
  373. If Mid(FolderPath, Len(FolderPath), 1) = "" Then FolderPath = Mid(FolderPath, 1, Len(FolderPath) - 1)
  374. FolderArray = Split(FolderPath, "")
  375. DiskName = FolderArray(0)
  376. Created = DiskName
  377. For i = 1 To UBound(FolderArray)
  378. Created = Created & "" & FolderArray(i)
  379. If Not FSO.FolderExists(Created) Then FSO.CreateFolder Created
  380. Next
  381. Set FSO = Nothing
  382. err.clear
  383. End Function
  384. End Class
复制代码
  1. <form action="save.asp" method="post" enctype="multipart/form-data">
  2. <input type="file" name="eimage" id="eimage" />
  3. <input name="commit" type="submit" value="提交" />
  4. </form>
复制代码
  1. <!--#include file="upload_class.asp"-->
  2. <%
  3. Dim Upload,path,tempCls
  4. set Upload=new AnUpLoad
  5. Upload.SingleSize=1024*1024*1024
  6. Upload.MaxSize=1024*1024*1024
  7. Upload.Exe="jpg|gif|bmp|png"
  8. Upload.Charset="utf-8"
  9. Upload.openProcesser=false
  10. Upload.GetData()
  11. if Upload.ErrorID>0 then
  12. response.write Upload.Description
  13. else
  14. dim said,stitle,scontent
  15. if Upload.files(-1).count>0 then
  16. path=server.mappath("/uploads")&"/"&Year(date())&"/"&Month(date())&"/"&Day(date())&"/"
  17. set tempCls=Upload.files("eimage")
  18. tempCls.SaveToFile path,0
  19. simage_name=tempCls.FileName
  20. simage_path="/uploads/"&Year(date())&"/"&Month(date())&"/"&Day(date())&"/"
  21. simage_url=simage_path & simage_name
  22. response.write "文件:" & tempCls.FileName & "上传完毕,大小为" & Upload.getsize(tempCls.Size) & ";原文件名" & tempCls.LocalName & "!<br />"
  23. set tempCls=nothing
  24. end if
  25. end if
  26. set Upload=nothing
  27. %>
复制代码


回复

使用道具 举报