Discuz教程网

为SWFUpload增加ASP版本的上传处理程序

[复制链接]
authicon dly 发表于 2011-9-8 20:17:41 | 显示全部楼层 |阅读模式
但也许是随着asp的逐渐淡出web开发,官方仅提供了.net、php等版本的上传处理程序,对于asp开发者来说则需要自行处理服务器端的数据接收。

刚接触此组件时就被它功能强大与灵活方便吸引,由于当时项目采用asp开发,百度一番后发现并无好用的asp上传处理程序(现在有很多啦^^),看来只能自己研究开发啦,最初采用处理普通上传的方法来截取文件的数据,几经测试发现并不能有效接收组件传递过来的文件数据,无奈只能着手分析下它发送的数据形式,通过分析发现它发送的数据格式还是和普通上传存在一些区别的,无论是图片还是文件都是以octet-stream形式发送到服务器的,了解了数据格式,剩下的就是截取啦,下面把我的处理方法分享给需要的朋友,处理速度还算理想。
  1. <%
  2. Class SWFUpload

  3. Private formData, folderPath, streamGet
  4. Private fileSize, chunkSize, bofCont, eofCont

  5. REM CLASS-INITIALIZE

  6. Private Sub Class_Initialize
  7. Call InitVariant
  8. Server.ScriptTimeOut = 1800
  9. Set streamGet = Server.CreateObject("ADODB.Stream")

  10. sAuthor = "51JS.COM-ZMM"
  11. sVersion = "Upload Class 1.0"
  12. End Sub

  13. REM CLASS-INITIALIZE

  14. Public Property Let SaveFolder(byVal sFolder)
  15. If Right(sFolder, 1) = "/" Then
  16. folderPath = sFolder
  17. Else
  18. folderPath = sFolder & "/"
  19. End If
  20. End Property

  21. Public Property Get SaveFolder
  22. SaveFolder = folderPath
  23. End Property

  24. Private Function InitVariant
  25. chunkSize = 1024 * 128

  26. folderPath = "/" : fileSize = 1024 * 10
  27. bofCont = StrToByte("octet-stream" & vbCrlf & vbCrlf)
  28. eofCont = StrToByte(vbCrlf & String(12, "-"))
  29. End Function

  30. Public Function GetUploadData
  31. Dim curRead : curRead = 0
  32. Dim dataLen : dataLen = Request.TotalBytes

  33. streamGet.Type = 1 : streamGet.Open
  34. Do While curRead < dataLen
  35. Dim partLen : partLen = chunkSize
  36. If partLen + curRead > dataLen Then partLen = dataLen - curRead
  37. streamGet.Write Request.BinaryRead(partLen)
  38. curRead = curRead + partLen
  39. Loop
  40. streamGet.Position = 0
  41. formData = streamGet.Read(dataLen)

  42. Call GetUploadFile
  43. End Function

  44. Public Function GetUploadFile
  45. Dim begMark : begMark = StrToByte("filename=")
  46. Dim begPath : begPath = InStrB(1, formData, begMark & ChrB(34)) + 10
  47. Dim endPath : endPath = InStrB(begPath, formData, ChrB(34))
  48. Dim cntPath : cntPath = MidB(formData, begPath, endPath - begPath)
  49. Dim cntName : cntName = folderPath & GetClientName(cntPath)

  50. Dim begFile : begFile = InStrB(1, formData, bofCont) + 15
  51. Dim endFile : endFile = InStrB(begFile, formData, eofCont)

  52. Call SaveUploadFile(cntName, begFile, endFile - begFile)
  53. End Function

  54. Public Function SaveUploadFile(byVal fName, byVal bCont, byVal sLen)
  55. Dim filePath : filePath = Server.MapPath(fName)
  56. If CreateFolder("|", GetParentFolder(filePath)) Then
  57. streamGet.Position = bCont
  58. Set streamPut = Server.CreateObject("ADODB.Stream")
  59. streamPut.Type = 1 : streamPut.Mode = 3 : streamPut.Open
  60. streamPut.Write streamGet.Read(sLen)
  61. streamPut.SaveToFile filePath, 2
  62. streamPut.Close : Set streamPut = Nothing
  63. End If
  64. End Function

  65. Private Function IsNothing(byVal sVar)
  66. IsNothing = IsNull(sVar) Or (sVar = Empty)
  67. End Function

  68. Private Function StrToByte(byVal sText)
  69. For i = 1 To Len(sText)
  70. StrToByte = StrToByte & ChrB(Asc(Mid(sText, i, 1)))
  71. Next
  72. End Function

  73. Private Function ByteToStr(byVal sByte)
  74. Dim streamTmp
  75. Set streamTmp = Server.CreateObject("ADODB.Stream")
  76. streamTmp.Type = 2
  77. streamTmp.Mode = 3
  78. streamTmp.Open
  79. streamTmp.WriteText sByte
  80. streamTmp.Position = 0
  81. streamTmp.CharSet = "utf-8"
  82. streamTmp.Position = 2
  83. ByteToStr = streamTmp.ReadText
  84. streamTmp.Close
  85. Set streamTmp = Nothing
  86. End Function

  87. Private Function GetClientName(byVal bInfo)
  88. Dim sInfo, regEx
  89. sInfo = ByteToStr(bInfo)
  90. If IsNothing(sInfo) Then
  91. GetClientName = ""
  92. Else
  93. Set regEx = New RegExp
  94. regEx.Pattern = "^.*\\([^\\]+)$"
  95. regEx.Global = False
  96. regEx.IgnoreCase = True
  97. GetClientName = regEx.Replace(sInfo, "$1")
  98. Set regEx = Nothing
  99. End If
  100. End Function

  101. Private Function GetParentFolder(byVal sPath)
  102. Dim regEx
  103. Set regEx = New RegExp
  104. regEx.Pattern = "^(.*)\\[^\\]*$"
  105. regEx.Global = True
  106. regEx.IgnoreCase = True
  107. GetParentFolder = regEx.Replace(sPath, "$1")
  108. Set regEx = Nothing
  109. End Function

  110. Private Function CreateFolder(byVal sLine, byVal sPath)
  111. Dim oFso
  112. Set oFso = Server.CreateObject("Scripting.FileSystemObject")
  113. If Not oFso.FolderExists(sPath) Then
  114. Dim regEx
  115. Set regEx = New RegExp
  116. regEx.Pattern = "^(.*)\\([^\\]*)$"
  117. regEx.Global = False
  118. regEx.IgnoreCase = True
  119. sLine = sLine & regEx.Replace(sPath, "$2") & "|"
  120. sPath = regEx.Replace(sPath, "$1")
  121. If CreateFolder(sLine, sPath) Then CreateFolder = True
  122. Set regEx = Nothing
  123. Else
  124. If sLine = "|" Then
  125. CreateFolder = True
  126. Else
  127. Dim sTemp : sTemp = Mid(sLine, 2, Len(sLine) - 2)
  128. If InStrRev(sTemp, "|") = 0 Then
  129. sLine = "|"
  130. sPath = sPath & "" & sTemp
  131. Else
  132. Dim Folder : Folder = Mid(sTemp, InStrRev(sTemp, "|") + 1)
  133. sLine = "|" & Mid(sTemp, 1, InStrRev(sTemp, "|") - 1) & "|"
  134. sPath = sPath & "" & Folder
  135. End If
  136. oFso.CreateFolder sPath
  137. If CreateFolder(sLine, sPath) Then CreateFolder = True
  138. End if
  139. End If
  140. Set oFso = Nothing
  141. End Function

  142. REM CLASS-TERMINATE

  143. Private Sub Class_Terminate
  144. streamGet.Close
  145. Set streamGet = Nothing
  146. End Sub

  147. End Class

  148. REM 调用方法
  149. Dim oUpload
  150. Set oUpload = New SWFUpload
  151. oUpload.SaveFolder = "存放路径"
  152. oUpload.GetUploadData
  153. Set oUpload = Nothing
  154. %>
复制代码



上一篇:ASP页面静态化批量生成代码分享(多种方法)
下一篇:SmartHTTP 简易HttpRequest类(ASP)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2024-5-3 14:05

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表