Uploader.Class.asp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. Public Function UploadRemote( url )
  84. Dim stream, filename
  85. filename = Right( url, Len(url) - InStrRev(url, "/") )
  86. Set stream = CrawlImage( url )
  87. If Not IsNull(stream) Then
  88. DoUpload stream, filename
  89. Else
  90. rsState = "Failed"
  91. End If
  92. Set stream = Nothing
  93. End Function
  94. Private Function DoUpload( stream, filename )
  95. rsFileSize = stream.Size
  96. If rsFileSize > cfgMaxSize Then
  97. rsState = stateString.Item( "SIZE_LIMIT_EXCCEED" )
  98. Exit Function
  99. End If
  100. rsOriginalFileName = filename
  101. fileType = GetExt(filename)
  102. If CheckExt(fileType) = False Then
  103. rsState = stateString.Item( "TYPE_NOW_ALLOW" )
  104. Exit Function
  105. End If
  106. Set formatter = new PathFormatter
  107. rsFilePath = formatter.format( cfgPathFormat, filename )
  108. savePath = Server.MapPath(rsFilePath)
  109. CheckOrCreatePath( GetDirectoryName(savePath) )
  110. stream.SaveToFile savePath
  111. stream.Close
  112. rsState = "SUCCESS"
  113. End Function
  114. Private Function GetDirectoryName(path)
  115. GetDirectoryName = Left( path, InStrRev(path, "\") )
  116. End Function
  117. Private Function Base64Decode( content )
  118. dim xml, stream, node
  119. Set xml = Server.CreateObject("MSXML2.DOMDocument")
  120. Set stream = Server.CreateObject("ADODB.Stream")
  121. Set node = xml.CreateElement("tmpNode")
  122. node.dataType = "bin.base64"
  123. node.Text = content
  124. stream.Charset = CURRENT_ENCODING
  125. stream.Type = 1
  126. stream.Open()
  127. stream.Write( node.nodeTypedValue )
  128. Set Base64Decode = stream
  129. Set node = Nothing
  130. Set stream = Nothing
  131. Set xml = Nothing
  132. End Function
  133. Private Function CrawlImage( url )
  134. Dim http, stream
  135. Set http = Server.CreateObject("Microsoft.XMLHTTP")
  136. http.Open "GET", url, false
  137. http.Send
  138. If http.Status = 200 Then
  139. Set stream = Server.CreateObject("ADODB.Stream")
  140. stream.Type = 1
  141. stream.Open()
  142. stream.Write http.ResponseBody
  143. Set CrawlImage = stream
  144. Else
  145. Set CrawlImage = null
  146. End If
  147. Set http = Nothing
  148. End Function
  149. Private Function CheckExt( fileType )
  150. If IsEmpty (cfgAllowType) Then
  151. CheckExt = true
  152. Exit Function
  153. End If
  154. For Each ext In cfgAllowType
  155. If UCase(fileType) = UCase(cfgAllowType.Item(ext)) Then
  156. CheckExt = true
  157. Exit Function
  158. End If
  159. Next
  160. CheckExt = false
  161. End Function
  162. Private Function GetExt( file )
  163. GetExt = Right( file, Len(file) - InStrRev(file, ".") + 1 )
  164. End Function
  165. Private Function CheckOrCreatePath( ByVal path )
  166. Set fs = Server.CreateObject("Scripting.FileSystemObject")
  167. Dim parts
  168. parts = Split( path, "\" )
  169. path = ""
  170. For Each part in parts
  171. path = path + part + "\"
  172. If fs.FolderExists( path ) = False Then
  173. fs.CreateFolder( path )
  174. End If
  175. Next
  176. End Function
  177. End Class
  178. %>