asp无组件上传图片并生成缩略图

本文介绍了如何使用ASP实现无组件的图片上传功能,包括创建上传页面、上传页处理以及保存文件和生成缩略图的代码。通过上传页upload.htm接收文件,然后在upfile.asp中进行大小和类型的检查,最后使用特定代码生成缩略图。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 
  1. 先创建一文件夹,并创建虚拟目录或站长点。
  2. 1.增加上传页xAdd.html
  3. <html>
  4. <head>
  5. <title>无组件上传</title>
  6. </head>
  7. <body>
  8. <form method="POST" name="myform" action="xSave.asp" target="_self">
  9. <input name="PicPath" type="text" id="PicPath" readonly="true">
  10. <input name="sPicPath" type="hidden" id="sPicPath">
  11. <iframe  id="Upload" src="upload.htm" frameborder=0 scrolling=no width="100%" height="20"></iframe>
  12. <img src="" id="objimg" style="display:none;" />
  13. </form>
  14. </body>
  15. </html>
  16. 2.上传页upload.htm
  17. <html>
  18. <head>
  19. <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
  20. <SCRIPT language=javascript>
  21. function check_file() 
  22. {
  23.   var strFileName=form.FileName.value;
  24.   if (strFileName=="")
  25.   {
  26.     alert("请选择要上传的文件");
  27.     return false;
  28.   }
  29. }
  30. </SCRIPT>
  31. </head>
  32. <body leftmargin="0" topmargin="0">
  33. <form action="upfile.asp" method="post" name="form1" enctype="multipart/form-data">
  34.   <input name="FileName" type="FILE" class="tx1" size="20" onChange="window.parent.document.getElementById('objimg').src=this.value;window.parent.document.getElementById('objimg').style.display='';">
  35.   <input type="submit" name="Submit" value="上传">
  36. </form>
  37. </body>
  38. </html>
  39. 3.上传保存代码页upfile.asp
  40. <!--#include file="upload.asp"-->
  41. <%
  42. Const MaxFileSize=300        '上传文件大小限制单位k
  43. Const UpFileType="gif|jpg|bmp|png"        '允许的上传文件类型
  44. set fs=createobject("scripting.filesystemobject"
  45. %>
  46. <html>
  47. <head>
  48. <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
  49. </head>
  50. <body>
  51. <%
  52. call upload_0()  '使用化境无组件上传类
  53. %>
  54. </body>
  55. </html>
  56. <%
  57. sub upload_0()    '使用化境无组件上传类
  58.     set upload=new upload_file    '建立上传对象
  59.     for each formName in upload.file '列出所有上传了的文件
  60.         set file=upload.file(formName)  '生成一个文件对象
  61.         if file.filesize<100 then
  62.             msg="请先选择你要上传的文件!"
  63.             founderr=true
  64.         end if
  65.         if file.filesize>(MaxFileSize*1024) then
  66.             msg="文件大小超过了限制,最大只能上传" & CStr(MaxFileSize) & "K的文件!"
  67.             founderr=true
  68.         end if
  69.         fileExt=lcase(file.FileExt)
  70.         Forumupload=split(UpFileType,"|")
  71.         for i=0 to ubound(Forumupload)
  72.             if fileEXT=trim(Forumupload(i)) then
  73.                 EnableUpload=true
  74.                 exit for
  75.             end if
  76.         next
  77.         if fileEXT="asp" or fileEXT="asa" or fileEXT="aspx" then
  78.             EnableUpload=false
  79.         end if
  80.         if EnableUpload=false then
  81.             msg="这种文件类型不允许上传!/n/n只允许上传这几种文件类型:" & UpFileType
  82.                         response.write"<SCRIPT language=JavaScript>alert('这种文件类型不允许上传!/n/n只允许上传这几种文件类型:" & UpFileType & "');"
  83.                         response.write"javascript:history.go(-1)</SCRIPT>"
  84.          founderr=true
  85.         end if
  86.         
  87.         strJS="<SCRIPT language=javascript>" & vbcrlf
  88.         if founderr<>true then
  89.             randomize
  90.             ranNum=int(900*rnd)+100
  91.             filename=year(now)&month(now)&day(now)&hour(now)&minute(now)&second(now)&ranNum"."
  92.             file.SaveToFile Server.mappath(FileName)   '保存文件
  93.             file_on=Server.mappath(FileName)
  94.             if fs.FileExists(file_on) then
  95.                 Set Jpeg = Server.CreateObject("Persits.Jpeg"
  96.                 Jpeg.Open file_on
  97.                 IW=Jpeg.OriginalWidth
  98.                 IH=Jpeg.OriginalHeight
  99.                 XH=130
  100.                 XW=130
  101.                 If IH>IW Then
  102.                     VW =cint( XH*IW/IH)
  103.                     VH=XH
  104.                 Else
  105.                     if IH=IW THEN
  106.                         VW=XW
  107.                         VH=XH
  108.                     ELSE
  109.                         VW = XW
  110.                         VH=cint(XW*IH/IW)
  111.                     end if
  112.                     
  113.                 End If
  114.                 Jpeg.Width = VW
  115.                 Jpeg.Height = VH
  116.                 fname1=split(Filename,"/")
  117.                 chsave="s"&fname1(Ubound(fname1))
  118.                 Jpeg.Save Server.MapPath(chsave)
  119.                 Jpeg.close
  120.                 Set Jpeg = nothing
  121.                 msg="保存缩位图成功! --"
  122.             else 
  123.                 msg="保存缩位图不成功!--"
  124.             end if
  125.             msg=msg"上传文件成功!"
  126.             FileType=right(fileExt,3)
  127.             strJS=strJS & "window.parent.document.getElementById('PicPath').value='" & replace(filename,"../","") & "';" & vbcrlf
  128.             strJS=strJS & "window.parent.document.getElementById('sPicPath').value='" & replace(chsave,"../","") & "';" & vbcrlf
  129.         end if
  130.         strJS=strJS & "alert('" & msg & "');" & vbcrlf
  131.         strJS=strJS & "history.go(-1);" & vbcrlf
  132.         strJS=strJS & "</script>"
  133.         response.write strJS
  134.         set file=nothing
  135.     next
  136.     set upload=nothing
  137. end sub
  138. %>
  139. 4.upload.asp页
  140. <%
  141. '----------------------------------------------------------------------
  142. '转发时请保留此声明信息,这段声明不并会影响你的速度!
  143. '*******************    无组件上传类   ********************************
  144. '声明:此上传类是在化境编程界发布的无组件上传类的基础上修改的.
  145. '在与化境编程界无组件上传类相比,速度快了将近50倍,当上传4M大小的文件时
  146. '服务器只需要10秒就可以处理完,是目前最快的无组件上传程序,当前版本为0.96
  147. '源代码公开,免费使用,对于商业用途,请与作者联系
  148. '文件属性:例如上传文件为c:/myfile/doc.txt
  149. 'FileName    文件名       字符串    "doc.txt"
  150. 'FileSize    文件大小     数值       1210
  151. 'FileType    文件类型     字符串    "text/plain"
  152. 'FileExt     文件扩展名   字符串    "txt"
  153. 'FilePath    文件原路径   字符串    "c:/myfile"
  154. '使用时注意事项:
  155. '由于Scripting.Dictionary区分大小写,所以在网页及ASP页的项目名都要相同的大小
  156. '写,如果人习惯用大写或小写,为了防止出错的话,可以把
  157. 'sFormName = Mid (sinfo,iFindStart,iFindEnd-iFindStart)
  158. '改为
  159. '(小写者)sFormName = LCase(Mid (sinfo,iFindStart,iFindEnd-iFindStart))
  160. '(大写者)sFormName = UCase(Mid (sinfo,iFindStart,iFindEnd-iFindStart))
  161. '**********************************************************************
  162. '----------------------------------------------------------------------
  163. dim oUpFileStream
  164. Class upload_file
  165.   
  166. dim Form,File,Version
  167.   
  168. Private Sub Class_Initialize 
  169.    '定义变量
  170.   dim RequestBinDate,sStart,bCrLf,sInfo,iInfoStart,iInfoEnd,tStream,iStart,oFileInfo
  171.   dim iFileSize,sFilePath,sFileType,sFormvalue,sFileName
  172.   dim iFindStart,iFindEnd
  173.   dim iFormStart,iFormEnd,sFormName
  174.    '代码开始
  175.   Version="无组件上传类 Version 0.96"
  176.   set Form = Server.CreateObject("Scripting.Dictionary")
  177.   set File = Server.CreateObject("Scripting.Dictionary")
  178.   if Request.TotalBytes < 1 then Exit Sub
  179.   set tStream = Server.CreateObject("adodb.stream")
  180.   set oUpFileStream = Server.CreateObject("adodb.stream")
  181.   oUpFileStream.Type = 1
  182.   oUpFileStream.Mode = 3
  183.   oUpFileStream.Open 
  184.   oUpFileStream.Write Request.BinaryRead(Request.TotalBytes)
  185.   oUpFileStream.Position=0
  186.   RequestBinDate = oUpFileStream.Read 
  187.   iFormEnd = oUpFileStream.Size
  188.   bCrLf = chrB(13) & chrB(10)
  189.   '取得每个项目之间的分隔符
  190.   sStart = MidB(RequestBinDate,1, InStrB(1,RequestBinDate,bCrLf)-1)
  191.   iStart = LenB (sStart)
  192.   iFormStart = iStart+2
  193.   '分解项目
  194.   Do
  195.     iInfoEnd = InStrB(iFormStart,RequestBinDate,bCrLf & bCrLf)+3
  196.     tStream.Type = 1
  197.     tStream.Mode = 3
  198.     tStream.Open
  199.     oUpFileStream.Position = iFormStart
  200.     oUpFileStream.CopyTo tStream,iInfoEnd-iFormStart
  201.     tStream.Position = 0
  202.     tStream.Type = 2
  203.     tStream.Charset ="gb2312"
  204.     sInfo = tStream.ReadText      
  205.     '取得表单项目名称
  206.     iFormStart = InStrB(iInfoEnd,RequestBinDate,sStart)-1
  207.     iFindStart = InStr(22,sInfo,"name=""",1)+6
  208.     iFindEnd = InStr(iFindStart,sInfo,"""",1)
  209.     sFormName = Mid (sinfo,iFindStart,iFindEnd-iFindStart)
  210.     '如果是文件
  211.     if InStr (45,sInfo,"filename=""",1) > 0 then
  212.       set oFileInfo= new FileInfo
  213.       '取得文件属性
  214.       iFindStart = InStr(iFindEnd,sInfo,"filename=""",1)+10
  215.       iFindEnd = InStr(iFindStart,sInfo,"""",1)
  216.       sFileName = Mid (sinfo,iFindStart,iFindEnd-iFindStart)
  217.       oFileInfo.FileName = GetFileName(sFileName)
  218.       oFileInfo.FilePath = GetFilePath(sFileName)
  219.       oFileInfo.FileExt = GetFileExt(sFileName)
  220.       iFindStart = InStr(iFindEnd,sInfo,"Content-Type: ",1)+14
  221.       iFindEnd = InStr(iFindStart,sInfo,vbCr)
  222.       oFileInfo.FileType = Mid (sinfo,iFindStart,iFindEnd-iFindStart)
  223.       oFileInfo.FileStart = iInfoEnd
  224.       oFileInfo.FileSize = iFormStart -iInfoEnd -2
  225.       oFileInfo.FormName = sFormName
  226.       file.add sFormName,oFileInfo
  227.     else
  228.     '如果是表单项目
  229.       tStream.Close
  230.       tStream.Type = 1
  231.       tStream.Mode = 3
  232.       tStream.Open
  233.       oUpFileStream.Position = iInfoEnd 
  234.       oUpFileStream.CopyTo tStream,iFormStart-iInfoEnd-2
  235.       tStream.Position = 0
  236.       tStream.Type = 2
  237.       tStream.Charset = "gb2312"
  238.       sFormvalue = tStream.ReadText 
  239.       form.Add sFormName,sFormvalue
  240.     end if
  241.     tStream.Close
  242.     iFormStart = iFormStart+iStart+2
  243.     '如果到文件尾了就退出
  244.     loop until (iFormStart+2) = iFormEnd 
  245.   RequestBinDate=""
  246.   set tStream = nothing
  247. End Sub
  248. Private Sub Class_Terminate  
  249.   '清除变量及对像
  250.   if not Request.TotalBytes<1 then
  251.     oUpFileStream.Close
  252.     set oUpFileStream =nothing
  253.     end if
  254.   Form.RemoveAll
  255.   File.RemoveAll
  256.   set Form=nothing
  257.   set File=nothing
  258. End Sub
  259.    
  260.  '取得文件路径
  261. Private function GetFilePath(FullPath)
  262.   If FullPath <> "" Then
  263.     GetFilePath = left(FullPath,InStrRev(FullPath, "/"))
  264.     Else
  265.     GetFilePath = ""
  266.   End If
  267. End function
  268. '取得文件名
  269. Private function GetFileName(FullPath)
  270.   If FullPath <> "" Then
  271.     GetFileName = mid(FullPath,InStrRev(FullPath, "/")+1)
  272.     Else
  273.     GetFileName = ""
  274.   End If
  275. End function
  276. '取得扩展名
  277. Private function GetFileExt(FullPath)
  278.   If FullPath <> "" Then
  279.     GetFileExt = mid(FullPath,InStrRev(FullPath, ".")+1)
  280.     Else
  281.     GetFileExt = ""
  282.   End If
  283. End function
  284. End Class
  285. '文件属性类
  286. Class FileInfo
  287.   dim FormName,FileName,FilePath,FileSize,FileType,FileStart,FileExt
  288.   Private Sub Class_Initialize 
  289.     FileName = ""
  290.     FilePath = ""
  291.     FileSize = 0
  292.     FileStart= 0
  293.     FormName = ""
  294.     FileType = ""
  295.     FileExt = ""
  296.   End Sub
  297.   
  298. '保存文件方法
  299.  Public function SaveToFile(FullPath)
  300.     dim oFileStream,ErrorChar,i
  301.     SaveToFile=1
  302.     if trim(fullpath)="" or right(fullpath,1)="/" then exit function
  303.     set oFileStream=CreateObject("Adodb.Stream")
  304.     oFileStream.Type=1
  305.     oFileStream.Mode=3
  306.     oFileStream.Open
  307.     oUpFileStream.position=FileStart
  308.     oUpFileStream.copyto oFileStream,FileSize
  309.     oFileStream.SaveToFile FullPath,2
  310.     oFileStream.Close
  311.     set oFileStream=nothing 
  312.     SaveToFile=0
  313.   end function
  314. End Class
  315. %>
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值