up_file.inc
上传用户:btntkt
上传日期:2021-04-16
资源大小:5296k
文件大小:10k
源码类别:

WEB源码(ASP,PHP,...)

开发平台:

DOS

  1. <%
  2. '--------------------------------------------------------------------
  3. '转发时请保留此声明信息,这段声明不并会影响你的速度!
  4. '*******************   无惧上传类 V1.0  *******************************
  5. '作者:梁无惧
  6. '网站:http://www.25cn.com
  7. '电子邮件:yjlrb@21cn.com
  8. '版权声明:版权所有,源代码公开,各种用途均可免费使用,但是修改后必须把修改后的文件
  9. '发送一份给作者.
  10. '上传类强化记录
  11. '修改者:Fssunwin
  12. '添加以下属性:
  13. 'InceptFileType 允许上传的文件类型,以英文逗号“,”分隔。
  14. '添加以下方法:
  15. 'FileWidth 图片宽度
  16. 'FileHeight 图片高度
  17. '*********************************************************************
  18. Dim oUpFileStream
  19. Class UpFile_Class
  20. Public Form,File,Version,Err
  21. Private CHK_FileType,CHK_MaxSize
  22. Private Sub Class_Initialize
  23. Version = "无惧上传类 Version V1.0"
  24. Err = -1
  25. CHK_FileType = ""
  26. CHK_MaxSize = -1
  27. Set Form = Server.CreateObject ("Scripting.Dictionary")
  28. Set File = Server.CreateObject ("Scripting.Dictionary")
  29. Set oUpFileStream = Server.CreateObject ("Adodb.Stream")
  30. Form.CompareMode = 1
  31. File.CompareMode = 1
  32. oUpFileStream.Type = 1
  33. oUpFileStream.Mode = 3
  34. oUpFileStream.Open
  35. End Sub
  36. Private Sub Class_Terminate  
  37. '清除变量及对像
  38. Form.RemoveAll
  39. Set Form = Nothing
  40. File.RemoveAll
  41. Set File = Nothing
  42. oUpFileStream.Close
  43. Set oUpFileStream = Nothing
  44. End Sub
  45. Public Property Get InceptFileType
  46. InceptFileType = CHK_FileType
  47. End Property
  48. Public Property Let InceptFileType(Byval vType)
  49. CHK_FileType = vType
  50. End Property
  51. Public Property Get MaxSize
  52. MaxSize = CHK_MaxSize
  53. End Property
  54. Public Property Let MaxSize(vSize)
  55. If IsNumeric(vSize) Then CHK_MaxSize = Int(vSize)
  56. End Property
  57. Public Sub GetDate()
  58.    '定义变量
  59.   Dim RequestBinDate,sSpace,bCrLf,sInfo,iInfoEnd,tStream,iStart,oFileInfo
  60.   Dim sFormValue,sFileName,sFormName,RequestSize
  61.   Dim iFindStart,iFindEnd,iFormStart,iFormEnd,FileBlag
  62.    '代码开始
  63.   RequestSize = Int(Request.TotalBytes)
  64.   If  RequestSize < 1 Then
  65. Err = 1
  66. Exit Sub
  67.   End If
  68.   Set tStream = Server.CreateObject ("Adodb.Stream")
  69.   oUpFileStream.Write Request.BinaryRead (RequestSize)
  70.   oUpFileStream.Position = 0
  71.   RequestBinDate = oUpFileStream.Read
  72.   iFormEnd = oUpFileStream.Size
  73.   
  74.   bCrLf = ChrB (13) & ChrB (10)
  75.   '取得每个项目之间的分隔符
  76.   sSpace = MidB (RequestBinDate,1, InStrB (1,RequestBinDate,bCrLf)-1)
  77.   iStart = LenB  (sSpace)
  78.   iFormStart = iStart+2
  79.   '分解项目
  80.   Do
  81.     iInfoEnd = InStrB (iFormStart,RequestBinDate,bCrLf & bCrLf)+3
  82.     tStream.Type = 1
  83.     tStream.Mode = 3
  84.     tStream.Open
  85.     oUpFileStream.Position = iFormStart
  86.     oUpFileStream.CopyTo tStream,iInfoEnd-iFormStart
  87.     tStream.Position = 0
  88.     tStream.Type = 2
  89.     tStream.CharSet = "gb2312"
  90.     sInfo = tStream.ReadText
  91.     '取得表单项目名称
  92.     iFormStart = InStrB (iInfoEnd,RequestBinDate,sSpace)-1
  93.     iFindStart = InStr(22,sInfo,"name=""",1)+6
  94.     iFindEnd = InStr(iFindStart,sInfo,"""",1)
  95.     sFormName = Mid(sinfo,iFindStart,iFindEnd-iFindStart)
  96.     '如果是文件
  97. If InStr(45,sInfo,"filename=""",1) > 0 Then
  98. Set oFileInfo = new FileInfo_Class
  99. '取得文件属性
  100. iFindStart = InStr(iFindEnd,sInfo,"filename=""",1)+10
  101. iFindEnd = InStr(iFindStart,sInfo,"""",1)
  102. sFileName = Mid(sinfo,iFindStart,iFindEnd-iFindStart)
  103. oFileInfo.FileName = Mid(sFileName,InStrRev(sFileName, "")+1)
  104. oFileInfo.FilePath = Left(sFileName,InStrRev(sFileName, ""))
  105. oFileInfo.FileExt = Lcase(Mid(sFileName,InStrRev(sFileName, ".")+1))
  106. iFindStart = InStr (iFindEnd,sInfo,"Content-Type: ",1)+14
  107. iFindEnd = InStr (iFindStart,sInfo,vbCr)
  108. oFileInfo.FileType = Ucase(Mid(sinfo,iFindStart,iFindEnd-iFindStart))
  109. oFileInfo.FileStart = iInfoEnd
  110. oFileInfo.FileSize = iFormStart -iInfoEnd -2
  111. oFileInfo.FormName = sFormName
  112. If Instr(oFileInfo.FileType,"IMAGE/") Or Instr(oFileInfo.FileType,"FLASH") Then
  113. FileBlag = GetImageSize
  114. oFileInfo.FileExt = FileBlag(0)
  115. oFileInfo.FileWidth = FileBlag(1)
  116. oFileInfo.FileHeight = FileBlag(2)
  117. FileBlag = Empty
  118. End If
  119. If CHK_MaxSize > 0 Then
  120. If oFileInfo.FileSize > CHK_MaxSize Then
  121. Err = 2
  122. Exit Sub
  123. End If
  124. End If
  125. If CheckErr(oFileInfo.FileExt) = False Then
  126. Exit Sub
  127. End If
  128. File.Add sFormName,oFileInfo
  129. Else
  130. '如果是表单项目
  131. tStream.Close
  132. tStream.Type = 1
  133. tStream.Mode = 3
  134. tStream.Open
  135. oUpFileStream.Position = iInfoEnd 
  136. oUpFileStream.CopyTo tStream,iFormStart-iInfoEnd-2
  137. tStream.Position = 0
  138. tStream.Type = 2
  139. tStream.CharSet = "gb2312"
  140. sFormValue = tStream.ReadText
  141. If Form.Exists (sFormName) Then _
  142. Form (sFormName) = Form (sFormName) & ", " & sFormValue _
  143. Else _
  144. Form.Add sFormName,sFormValue
  145. End If
  146. tStream.Close
  147. iFormStart = iFormStart+iStart+2
  148.   '如果到文件尾了就退出
  149.   Loop Until  (iFormStart+2) = iFormEnd
  150.   RequestBinDate = ""
  151.   Set tStream = Nothing
  152. End Sub
  153. '====================================================================
  154. '验证上传类型
  155. '====================================================================
  156. Private Function CheckErr(Byval ChkExt)
  157. CheckErr=False
  158. If CHK_FileType = "" Then CheckErr=True : Exit Function
  159. Dim ChkStr
  160. ChkStr = ","&Lcase(CHK_FileType)&","
  161. If Instr(ChkStr,","&ChkExt&",")>0 Then
  162. CheckErr=True
  163. Else
  164. Err = 3
  165. End If
  166. End Function
  167. '====================================================================
  168. '图像宽高类型读取
  169. '====================================================================
  170. Private Function Bin2Str(Byval Bin)
  171. Dim i, Str, Sclow
  172. For i = 1 To LenB(Bin)
  173. Sclow = MidB(Bin,i,1)
  174. If ASCB(Sclow)<128 Then
  175. Str = Str & Chr(ASCB(Sclow))
  176. Else
  177. i = i+1
  178. If i <= LenB(Bin) Then Str = Str & Chr(ASCW(MidB(Bin,i,1)&Sclow))
  179. End If
  180. Next 
  181. Bin2Str = Str
  182. End Function
  183. Private Function Num2Str(Byval num,Byval Base,Byval Lens)
  184. Dim ImageSize
  185. ImageSize = ""
  186. While(num>=Base)
  187. ImageSize = (num mod Base) & ImageSize
  188. num = (num - num mod Base)/Base
  189. Wend
  190. Num2Str = Right(String(Lens,"0") & num & ImageSize,Lens)
  191. End Function
  192. Private Function Str2Num(Byval str,Byval Base)
  193. Dim ImageSize,i
  194. ImageSize = 0
  195. For i=1 To Len(str)
  196. ImageSize = ImageSize *Base + Cint(Mid(str,i,1))
  197. Next
  198. Str2Num = ImageSize
  199. End Function
  200. Private Function BinVal(Byval bin)
  201. Dim ImageSize,i
  202. ImageSize = 0
  203. For i = lenb(bin) To 1 Step -1
  204. ImageSize = ImageSize *256 + ASCB(Midb(bin,i,1))
  205. Next
  206. BinVal = ImageSize
  207. End Function
  208. Private Function BinVal2(Byval bin)
  209. Dim ImageSize,i
  210. ImageSize = 0
  211. For i = 1 To Lenb(bin)
  212. ImageSize = ImageSize *256 + ASCB(Midb(bin,i,1))
  213. Next
  214. BinVal2 = ImageSize
  215. End Function
  216. Private Function GetImageSize() 
  217. Dim ImageSize(2),bFlag
  218. bFlag = oUpFileStream.Read(3)
  219. Select Case Hex(BinVal(bFlag))
  220. Case "4E5089":
  221. oUpFileStream.Read(15)
  222. ImageSize(0) = "png"
  223. ImageSize(1) = BinVal2(oUpFileStream.Read(2))
  224. oUpFileStream.Read(2)
  225. ImageSize(2) = BinVal2(oUpFileStream.Read(2))
  226. Case "464947":
  227. oUpFileStream.Read(3)
  228. ImageSize(0) = "gif"
  229. ImageSize(1) = BinVal(oUpFileStream.Read(2))
  230. ImageSize(2) = BinVal(oUpFileStream.Read(2))
  231. Case "535746":
  232. Dim BinData,sConv,nBits
  233. oUpFileStream.Read(5)
  234. BinData = oUpFileStream.Read(1)
  235. sConv = Num2Str(ASCB(BinData),2 ,8)
  236. nBits = Str2Num(Left(sConv,5),2)
  237. sConv = Mid(sConv,6)
  238. While(Len(sConv)<nBits*4)
  239. BinData = oUpFileStream.Read(1)
  240. sConv = sConv&Num2Str(ASCB(BinData),2 ,8)
  241. Wend
  242. ImageSize(0) = "swf"
  243. ImageSize(1) = Int(ABS(Str2Num(Mid(sConv,1*nBits+1,nBits),2)-Str2Num(Mid(sConv,0*nBits+1,nBits),2))/20)
  244. ImageSize(2) = Int(ABS(Str2Num(Mid(sConv,3*nBits+1,nBits),2)-Str2Num(Mid(sConv,2*nBits+1,nBits),2))/20)
  245. Case "535743":'flashmx
  246. ImageSize(0) = "swf"
  247. ImageSize(1) = 0
  248. ImageSize(2) = 0
  249. Case "FFD8FF":
  250. Dim p1
  251. Do 
  252. Do: p1 = BinVal(oUpFileStream.Read(1)): Loop While p1 = 255 And Not oUpFileStream.EOS
  253. If p1>191 and p1<196 Then Exit Do Else oUpFileStream.Read(BinVal2(oUpFileStream.Read(2))-2)
  254. Do:p1 = BinVal(oUpFileStream.Read(1)):Loop While p1<255 And Not oUpFileStream.EOS
  255. Loop While True
  256. oUpFileStream.Read(3)
  257. ImageSize(0) = "jpg"
  258. ImageSize(2) = BinVal2(oUpFileStream.Read(2))
  259. ImageSize(1) = BinVal2(oUpFileStream.Read(2))
  260. Case Else:
  261. If Left(Bin2Str(bFlag),2) = "BM" Then
  262. oUpFileStream.Read(15)
  263. ImageSize(0) = "bmp"
  264. ImageSize(1) = BinVal(oUpFileStream.Read(4))
  265. ImageSize(2) = BinVal(oUpFileStream.Read(4))
  266. Else
  267. ImageSize(0) = "(UNKNOWN)"
  268. End If
  269. End Select
  270. GetImagesize = ImageSize
  271. End Function
  272. End Class
  273. '文件属性类
  274. Class FileInfo_Class
  275. Public FormName,FileName,FilePath,FileSize,FileType,FileStart,FileExt,FileWidth,FileHeight
  276. Private Sub Class_Initialize
  277. FileWidth=0
  278. FileHeight=0
  279. End Sub
  280. '保存文件方法
  281. Public Sub SaveToFile (Byval Path)
  282. Dim Ext,oFileStream
  283. Ext = LCase(Mid(Path, InStrRev(Path, ".") + 1))
  284. If Ext <> FileExt Then Exit Sub
  285. If Trim(Path)="" or FileStart=0 or FileName="" or Right(Path,1)="/" Then Exit Sub
  286. 'On Error Resume Next
  287. Set oFileStream = CreateObject ("Adodb.Stream")
  288. oFileStream.Type = 1
  289. oFileStream.Mode = 3
  290. oFileStream.Open
  291. oUpFileStream.Position = FileStart
  292. oUpFileStream.CopyTo oFileStream,FileSize
  293. oFileStream.SaveToFile Path,2
  294. oFileStream.Close
  295. Set oFileStream = Nothing 
  296. End Sub
  297. '取得文件数据
  298. Public Function FileData
  299. oUpFileStream.Position = FileStart
  300. FileData = oUpFileStream.Read (FileSize)
  301. End Function
  302. End Class
  303. %>