Uploader.Class.asp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <!--#include file="PathFormatter.class.asp"-->
  2. <!--#include file="MultiformProcessor.class.asp"-->
  3. <%
  4. ' ASP 文件上传类
  5. ' Author: techird
  6. ' Email: techird@qq.com
  7. '配置
  8. 'MAX_SIZE 在这里设定了之后如果出现大上传失败,请执行以下步骤
  9. 'IIS 6
  10. '找到位于 C:\Windows\System32\Inetsrv 中的 metabase.XML 打开,找到ASPMaxRequestEntityAllowed 把他修改为需要的值(如10240000即10M)
  11. 'IIS 7
  12. '打开IIS控制台,选择 ASP,在限制属性里有一个“最大请求实体主题限制”,设置需要的值
  13. CURRENT_ENCODING = "gb2312"
  14. Class Uploader
  15. '上传配置
  16. Private cfgMaxSize
  17. Private cfgAllowType
  18. Private cfgPathFormat
  19. Private cfgFileField
  20. '上传返回信息
  21. Private stateString
  22. Private rsOriginalFileName
  23. Private rsFilePath
  24. Private rsFileName
  25. Private rsFileSize
  26. Private rsState
  27. Private rsFormValues
  28. Private Sub Class_Initialize
  29. Set stateString = Server.CreateObject("Scripting.Dictionary")
  30. stateString.Add "SIZE_LIMIT_EXCCEED", "File size exceeded!"
  31. stateString.Add "TYPE_NOW_ALLOW", "File type not allowed!"
  32. End Sub
  33. Public Property Let MaxSize(ByVal size)
  34. cfgMaxSize = size
  35. End Property
  36. Public Property Let AllowType(ByVal types)
  37. Set cfgAllowType = types
  38. End Property
  39. Public Property Let PathFormat(ByVal format)
  40. cfgPathFormat = format
  41. End Property
  42. Public Property Let FileField(ByVal field)
  43. cfgFileField = field
  44. End Property
  45. Public Property Get OriginalFileName
  46. OriginalFileName = rsOriginalFileName
  47. End Property
  48. Public Property Get FileName
  49. FileName = rsFileName
  50. End Property
  51. Public Property Get FilePath
  52. FilePath = rsFilePath
  53. End Property
  54. Public Property Get FileSize
  55. FileSize = rsFileSize
  56. End Property
  57. Public Property Get State
  58. State = rsState
  59. End Property
  60. Public Property Get FormValues
  61. Set FormValues = rsFormValues
  62. End Property
  63. Public Function UploadForm()
  64. ProcessForm()
  65. SaveFile()
  66. End Function
  67. Public Function ProcessForm()
  68. Set processor = new MultiformProcessor
  69. Set rsFormValues = processor.Process()
  70. End Function
  71. Public Function SaveFile()
  72. Dim stream, filename
  73. Set stream = rsFormValues.Item( cfgFileField )
  74. filename = rsFormValues.Item( "filename" )
  75. DoUpload stream, filename
  76. End Function
  77. Public Function UploadBase64( filename )
  78. Dim stream, content
  79. content = Request.Item ( cfgFileField )
  80. Set stream = Base64Decode( content )
  81. DoUpload stream, filename
  82. End Function
  83. Private Function RegExpTest(patrn, str)
  84. Dim regEx, Match, Matches
  85. Set regEx = New RegExp
  86. regEx.Pattern = patrn
  87. regEx.IgnoreCase = False
  88. regEx.Global = True
  89. Set Matches = regEx.Execute(str)
  90. For Each Match in Matches
  91. RetStr = RetStr & Match.value &" "
  92. RetStr = RetStr & vbCRLF
  93. Next
  94. RegExpTest = RetStr
  95. End Function
  96. Private Function IpToNumber( ip )
  97. arr=split(ip,".")
  98. IpToNumber=256*256*256*clng(arr(0))+256*256*clng(arr(1))+256*clng(arr(2))+clng(arr(3))
  99. End Function
  100. Private Function IsPrivateIp( url )
  101. Dim ip
  102. ip = RegExpTest("\d+\.\d+\.\d+\.\d*", url)
  103. If ip = "" Then
  104. If RegExpTest("([\w-]+\.)+[\w-]+", url) <> "" Then
  105. IsPrivateIp = False:Exit Function
  106. End If
  107. IsPrivateIp = True:Exit Function
  108. End If
  109. If instr(ip,"127.")=1 Then
  110. IsPrivateIp = true:Exit Function
  111. End If
  112. ABegin = IpToNumber("10.0.0.0"):AEnd = IpToNumber("10.255.255.255")
  113. BBegin = IpToNumber("172.16.0.0"):BEnd = IpToNumber("172.31.255.255")
  114. CBegin = IpToNumber("192.168.0.0"):CEnd = IpToNumber("192.168.255.255")
  115. IpNum = IpToNumber(ip)
  116. IsPrivateIp = (ABegin <= IpNum and IpNum <= AEnd) or (BBegin <= IpNum and IpNum <= BEnd) or (CBegin <= IpNum and IpNum <= CEnd)
  117. End Function
  118. Public Function UploadRemote( url )
  119. Dim stream, filename
  120. If IsPrivateIp(url) Then
  121. rsState = "Failed":Exit Function
  122. End If
  123. filename = Right( url, Len(url) - InStrRev(url, "/") )
  124. Set stream = CrawlImage( url )
  125. If Not IsNull(stream) Then
  126. DoUpload stream, filename
  127. Else
  128. rsState = "Failed"
  129. End If
  130. Set stream = Nothing
  131. End Function
  132. Private Function DoUpload( stream, filename )
  133. rsFileSize = stream.Size
  134. If rsFileSize > cfgMaxSize Then
  135. rsState = stateString.Item( "SIZE_LIMIT_EXCCEED" )
  136. Exit Function
  137. End If
  138. rsOriginalFileName = filename
  139. fileType = GetExt(filename)
  140. If CheckExt(fileType) = False Then
  141. rsState = stateString.Item( "TYPE_NOW_ALLOW" )
  142. Exit Function
  143. End If
  144. Set formatter = new PathFormatter
  145. rsFilePath = formatter.format( cfgPathFormat, filename )
  146. savePath = Server.MapPath(rsFilePath)
  147. CheckOrCreatePath( GetDirectoryName(savePath) )
  148. stream.SaveToFile savePath
  149. stream.Close
  150. rsState = "SUCCESS"
  151. End Function
  152. Private Function GetDirectoryName(path)
  153. GetDirectoryName = Left( path, InStrRev(path, "\") )
  154. End Function
  155. Private Function Base64Decode( content )
  156. dim xml, stream, node
  157. Set xml = Server.CreateObject("MSXML2.DOMDocument")
  158. Set stream = Server.CreateObject("ADODB.Stream")
  159. Set node = xml.CreateElement("tmpNode")
  160. node.dataType = "bin.base64"
  161. node.Text = content
  162. stream.Charset = CURRENT_ENCODING
  163. stream.Type = 1
  164. stream.Open()
  165. stream.Write( node.nodeTypedValue )
  166. Set Base64Decode = stream
  167. Set node = Nothing
  168. Set stream = Nothing
  169. Set xml = Nothing
  170. End Function
  171. Private Function CrawlImage( url )
  172. Dim http, stream
  173. Set http = Server.CreateObject("Microsoft.XMLHTTP")
  174. http.Open "GET", url, false
  175. http.Send
  176. If http.Status = 200 Then
  177. Set stream = Server.CreateObject("ADODB.Stream")
  178. stream.Type = 1
  179. stream.Open()
  180. stream.Write http.ResponseBody
  181. Set CrawlImage = stream
  182. Else
  183. Set CrawlImage = null
  184. End If
  185. Set http = Nothing
  186. End Function
  187. Private Function CheckExt( fileType )
  188. If IsEmpty (cfgAllowType) Then
  189. CheckExt = true
  190. Exit Function
  191. End If
  192. For Each ext In cfgAllowType
  193. If UCase(fileType) = UCase(cfgAllowType.Item(ext)) Then
  194. CheckExt = true
  195. Exit Function
  196. End If
  197. Next
  198. CheckExt = false
  199. End Function
  200. Private Function GetExt( file )
  201. GetExt = Right( file, Len(file) - InStrRev(file, ".") + 1 )
  202. End Function
  203. Private Function CheckOrCreatePath( ByVal path )
  204. Set fs = Server.CreateObject("Scripting.FileSystemObject")
  205. Dim parts
  206. parts = Split( path, "\" )
  207. path = ""
  208. For Each part in parts
  209. path = path + part + "\"
  210. If fs.FolderExists( path ) = False Then
  211. fs.CreateFolder( path )
  212. End If
  213. Next
  214. End Function
  215. End Class
  216. %>