[原创]ASP无组件上载,带进度条,多文件上载。。

61 篇文章 1 订阅
13 篇文章 0 订阅

各种ASP无组件上载,带进度条的实在太多,用起来也不方便,今天自己写了一个,希望大家给点意见 页面都是UTF-8 编码的,用的时候可根据自己页面编码转换

核心编码:SundyUpload.asp 例子:Example.asp SundyUpload.asp代码如下:

 

  1. <!--
  2.  *  Sundy Upload class, version 1.0.0
  3.  *  (c) 2006-2008 feng_sundy
  4.  *  
  5.  *  For details, see the feng_sundy blog: http://blog.csdn.net/feng_sundy/ *
  6.  *
  7. -->
  8. <%If 1=2 Then%><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><%
  9. End If
  10. Dim SundyUpload_SourceData
  11. Class SundyUpload
  12.   Dim objForm,objFile,Version,objProgress
  13.     Dim xmlPath,CharsetEncoding
  14.   Public Function Form(strForm)
  15.     strForm=lcase(strForm)
  16.     If NOT objForm.exists(strForm) Then
  17.       Form=""
  18.     Else
  19.       Form=objForm(strForm)
  20.     End If
  21.   End Function
  22.   Public Function File(strFile)
  23.     strFile=lcase(strFile)
  24.     If NOT objFile.exists(strFile) Then
  25.       Set File=new FileInfo
  26.     Else
  27.       Set File=objFile(strFile)
  28.     End If
  29.   End Function
  30.   
  31.     Public Sub UploadInit(progressXmlPath,charset)
  32.     Dim RequestData,sStart,Crlf,sInfo,iInfoStart,iInfoEnd,tStream,iStart,theFile
  33.         Dim iFileSize,sFilePath,sFileType,sFormValue,sFileName
  34.         Dim iFindStart,iFindEnd
  35.         Dim iFormStart,iFormEnd,sFormName
  36.       
  37.         Version="Upload Width Progress Bar Version 1.0"
  38.         Set objForm=Server.CreateObject("Scripting.Dictionary")
  39.         Set objFile=Server.CreateObject("Scripting.Dictionary")
  40.         If Request.TotalBytes<1 Then Exit Sub
  41.         Set tStream = Server.CreateObject("adodb.stream")
  42.         Set SundyUpload_SourceData = Server.CreateObject("adodb.stream")
  43.         SundyUpload_SourceData.Type = 1
  44.         SundyUpload_SourceData.Mode =3
  45.         SundyUpload_SourceData.Open
  46.         
  47.         Dim TotalBytes
  48.         Dim ChunkReadSize
  49.         Dim DataPart, PartSize
  50.         Dim objProgress
  51.         
  52.         TotalBytes = Request.TotalBytes  'total requst size
  53.         ChunkReadSize = 64 * 1024   ' block size 64K
  54.         BytesRead = 0
  55.         xmlPath = progressXmlPath
  56.         CharsetEncoding = charset
  57.         If CharsetEncoding = "" Then
  58.           CharsetEncoding = "utf-8"
  59.         End If
  60.         Set objProgress = New Progress
  61.         objProgress.ProgressInit(xmlPath)
  62.         objProgress.UpdateProgress Totalbytes,0
  63.         'Loop read block
  64.         Do While BytesRead < TotalBytes
  65.             PartSize = ChunkReadSize
  66.             If PartSize + BytesRead > TotalBytes Then PartSize = TotalBytes - BytesRead
  67.             DataPart = Request.BinaryRead(PartSize)
  68.             BytesRead = BytesRead + PartSize
  69.     
  70.             SundyUpload_SourceData.Write DataPart
  71.             
  72.             objProgress.UpdateProgress Totalbytes,BytesRead 
  73.         Loop
  74.         'SundyUpload_SourceData.Write  Request.BinaryRead(Request.TotalBytes)
  75.         SundyUpload_SourceData.Position=0
  76.         RequestData =SundyUpload_SourceData.Read 
  77.     
  78.         iFormStart = 1
  79.         iFormEnd = LenB(RequestData)
  80.         Crlf = chrB(13) & chrB(10)
  81.         sStart = MidB(RequestData,1, InStrB(iFormStart,RequestData,Crlf)-1)
  82.         iStart = LenB (sStart)
  83.         iFormStart=iFormStart+iStart+1
  84.         While (iFormStart + 10) < iFormEnd 
  85.             iInfoEnd = InStrB(iFormStart,RequestData,Crlf & Crlf)+3
  86.             tStream.Type = 1
  87.             tStream.Mode =3
  88.             tStream.Open
  89.             SundyUpload_SourceData.Position = iFormStart
  90.             SundyUpload_SourceData.CopyTo tStream,iInfoEnd-iFormStart
  91.             tStream.Position = 0
  92.             tStream.Type = 2
  93.             tStream.Charset =CharsetEncoding
  94.             sInfo = tStream.ReadText
  95.             tStream.Close
  96.             'get form item name
  97.             iFormStart = InStrB(iInfoEnd,RequestData,sStart)
  98.             iFindStart = InStr(22,sInfo,"name=""",1)+6
  99.             iFindEnd = InStr(iFindStart,sInfo,"""",1)
  100.             sFormName = lcase(Mid (sinfo,iFindStart,iFindEnd-iFindStart))
  101.             'if file form
  102.             If InStr (45,sInfo,"filename=""",1) > 0 Then
  103.                 Set theFile=new FileInfo
  104.                 'get file name
  105.                 iFindStart = InStr(iFindEnd,sInfo,"filename=""",1)+10
  106.                 iFindEnd = InStr(iFindStart,sInfo,"""",1)
  107.                 sFileName = Mid (sinfo,iFindStart,iFindEnd-iFindStart)
  108.                 theFile.FileName=getFileName(sFileName)
  109.                 theFile.FilePath=getFilePath(sFileName)
  110.                 'get file type
  111.                 iFindStart = InStr(iFindEnd,sInfo,"Content-Type: ",1)+14
  112.                 iFindEnd = InStr(iFindStart,sInfo,vbCr)
  113.                 theFile.FileType =Mid (sinfo,iFindStart,iFindEnd-iFindStart)
  114.                 theFile.FileStart =iInfoEnd
  115.                 theFile.FileSize = iFormStart -iInfoEnd -3
  116.                 theFile.FormName=sFormName
  117.                 If NOT objFile.Exists(sFormName) Then
  118.                     objFile.add sFormName,theFile
  119.                 End If
  120.             Else
  121.                 'if normal form item
  122.                 tStream.Type =1
  123.                 tStream.Mode =3
  124.                 tStream.Open
  125.                 SundyUpload_SourceData.Position = iInfoEnd 
  126.                 SundyUpload_SourceData.CopyTo tStream,iFormStart-iInfoEnd-3
  127.                 tStream.Position = 0
  128.                 tStream.Type = 2
  129.                 tStream.Charset = CharsetEncoding
  130.                 sFormValue = tStream.ReadText 
  131.                 tStream.Close
  132.                 If objForm.Exists(sFormName) Then
  133.                     objForm(sFormName)=objForm(sFormName)", "&sFormValue         
  134.                 Else
  135.                     objForm.Add sFormName,sFormValue
  136.                 End If
  137.             End If
  138.             iFormStart=iFormStart+iStart+1
  139.         Wend
  140.         RequestData=""
  141.         Set tStream = Nothing     
  142.     End Sub
  143.     Private Sub Class_Initialize 
  144.         
  145.     End Sub
  146.     
  147.     Private Sub Class_Terminate  
  148.       If Request.TotalBytes>0 Then
  149.             objForm.RemoveAll
  150.             objFile.RemoveAll
  151.             Set objForm=Nothing
  152.             Set objFile=Nothing
  153.             SundyUpload_SourceData.Close
  154.             Set SundyUpload_SourceData = Nothing
  155.       End If
  156.         Set objProgress = Nothing
  157.         Set objFso = Server.CreateObject("Scripting.FileSystemObject")
  158.         If objFso.FileExists(xmlPath) Then
  159.           objFso.DeleteFile(xmlPath)
  160.         End If
  161.         Set objFso = Nothing
  162.     End Sub
  163.     Private Function GetFilePath(FullPath)
  164.         If FullPath <> "" Then
  165.           GetFilePath = left(FullPath,InStrRev(FullPath, "/"))
  166.         Else
  167.           GetFilePath = ""
  168.         End If
  169.     End Function
  170.     Private Function GetFileName(FullPath)
  171.         If FullPath <> "" Then
  172.           GetFileName = mid(FullPath,InStrRev(FullPath, "/")+1)
  173.         Else
  174.           GetFileName = ""
  175.         End If
  176.     End Function
  177. End Class
  178. Class FileInfo
  179.   Dim FormName,FileName,FilePath,FileSize,FileType,FileStart
  180.   Private Sub Class_Initialize 
  181.     FileName = ""
  182.     FilePath = ""
  183.     FileSize = 0
  184.     FileStart= 0
  185.     FormName = ""
  186.     FileType = ""
  187.   End Sub
  188.   
  189.     Public Function SaveAs(FullPath)
  190.         Dim dr,ErrorChar,i
  191.         SaveAs=True
  192.         If trim(fullpath)="" or FileStart=0 or FileName="" or right(fullpath,1)="/" Then Exit Function
  193.         Set dr=CreateObject("Adodb.Stream")
  194.         dr.Mode=3
  195.         dr.Type=1
  196.         dr.Open
  197.         SundyUpload_SourceData.position=FileStart
  198.         SundyUpload_SourceData.copyto dr,FileSize
  199.         dr.SaveToFile FullPath,2
  200.         dr.Close
  201.         Set dr=Nothing 
  202.         SaveAs=False
  203.     End Function
  204. End Class
  205. Class Progress
  206.   Dim objDom,xmlPath
  207.     Dim startTime
  208.   Private Sub Class_Initialize
  209.     End Sub
  210.     
  211.     Public Sub ProgressInit(xmlPathTmp)
  212.       Dim objRoot,objChild
  213.         Dim objPI
  214.         xmlPath = xmlPathTmp
  215.         Set objDom = Server.CreateObject("Microsoft.XMLDOM")
  216.         Set objRoot = objDom.createElement("progress")
  217.         objDom.appendChild objRoot
  218.         
  219.         Set objChild = objDom.createElement("totalbytes")
  220.         objChild.Text = "0"
  221.         objRoot.appendChild objChild
  222.         Set objChild = objDom.createElement("uploadbytes")
  223.         objChild.Text = "0"
  224.         objRoot.appendChild objChild
  225.         Set objChild = objDom.createElement("uploadpercent")
  226.         objChild.Text = "0%"
  227.         objRoot.appendChild objChild
  228.         Set objChild = objDom.createElement("uploadspeed")
  229.         objChild.Text = "0"
  230.         objRoot.appendChild objChild
  231.         Set objChild = objDom.createElement("totaltime")
  232.         objChild.Text = "00:00:00"
  233.         objRoot.appendChild objChild
  234.         Set objChild = objDom.createElement("lefttime")
  235.         objChild.Text = "00:00:00"
  236.         objRoot.appendChild objChild
  237.         
  238.         Set objPI = objDom.createProcessingInstruction("xml","version='1.0' encoding='utf-8'")
  239.         objDom.insertBefore objPI, objDom.childNodes(0)
  240.         objDom.Save xmlPath
  241.         Set objPI = Nothing
  242.         Set objChild = Nothing
  243.         Set objRoot = Nothing
  244.         Set objDom = Nothing
  245.     End Sub
  246.     
  247.     Sub UpdateProgress(tBytes,rBytes)
  248.       Dim eTime,currentTime,speed,totalTime,leftTime,percent
  249.         If rBytes = 0 Then
  250.             startTime = Timer
  251.             Set objDom = Server.CreateObject("Microsoft.XMLDOM")
  252.             objDom.load(xmlPath)
  253.             objDom.selectsinglenode("//totalbytes").text=tBytes
  254.             objDom.save(xmlPath)
  255.         Else
  256.           speed = 0.0001
  257.           currentTime = Timer
  258.         eTime = currentTime - startTime
  259.             If eTime>0 Then speed = rBytes / eTime
  260.             totalTime = tBytes / speed
  261.             leftTime = (tBytes - rBytes) / speed
  262.             percent = Round(rBytes *100 / tBytes)
  263.             'objDom.selectsinglenode("//uploadbytes").text = rBytes
  264.             'objDom.selectsinglenode("//uploadspeed").text = speed
  265.             'objDom.selectsinglenode("//totaltime").text = totalTime
  266.             'objDom.selectsinglenode("//lefttime").text = leftTime
  267.             objDom.selectsinglenode("//uploadbytes").text = FormatFileSize(rBytes) & " / " & FormatFileSize(tBytes)
  268.             objDom.selectsinglenode("//uploadpercent").text = percent
  269.             objDom.selectsinglenode("//uploadspeed").text = FormatFileSize(speed) & "/sec"
  270.             objDom.selectsinglenode("//totaltime").text = SecToTime(totalTime)
  271.             objDom.selectsinglenode("//lefttime").text = SecToTime(leftTime)
  272.             objDom.save(xmlPath)        
  273.         End If
  274.     End Sub
  275.     private Function SecToTime(sec)
  276.         Dim h:h = "0"
  277.         Dim m:m = "0"
  278.         Dim s:s = "0"
  279.         h = round(sec / 3600)
  280.         m = round( (sec mod 3600) / 60)
  281.         s = round(sec mod 60)
  282.         If LEN(h)=1 Then h = "0" & h
  283.         If LEN(m)=1 Then m = "0" & m
  284.         If LEN(s)=1 Then s = "0" & s
  285.         SecToTime = (h & ":" & m & ":" & s)
  286.     End Function
  287.         
  288.     private Function FormatFileSize(fsize)
  289.         Dim radio,k,m,g,unitTMP
  290.         k = 1024
  291.         m = 1024*1024
  292.         g = 1024*1024*1024
  293.         radio = 1
  294.         If Fix(fsize / g) > 0.0 Then
  295.             unitTMP = "GB"
  296.             radio = g
  297.         ElseIf Fix(fsize / m) > 0 Then
  298.             unitTMP = "MB"
  299.             radio = m
  300.         ElseIf Fix(fsize / k) > 0 Then
  301.             unitTMP = "KB"
  302.             radio = k
  303.         Else
  304.             unitTMP = "B"
  305.             radio = 1
  306.         End If
  307.         If radio = 1 Then
  308.             FormatFileSize = fsize & " " & unitTMP
  309.         Else
  310.             FormatFileSize = FormatNumber(fsize/radio,3) & unitTMP
  311.         End If
  312.     End Function
  313.     Private Sub Class_Terminate  
  314.       Set objDom = Nothing
  315.     End Sub
  316. End Class
  317. %>
  318. <script language="javascript">
  319. //start progress bar
  320. function startProgress(xmlPath){
  321.   displayProgress();
  322.     setProgressDivPos();
  323.     setTimeout("DisplayProgressBar('" + xmlPath + "')",500);
  324. }
  325. function DisplayProgressBar(xmlPath){
  326.     var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
  327.     xmlDoc.async = false;
  328.     xmlDoc.load(xmlPath);
  329.     if (xmlDoc.parseError.errorCode!=0){
  330.         var error = xmlDoc.parseError;
  331.         //alert(error.reason)
  332.         setTimeout("DisplayProgressBar('" + xmlPath + "')",1000);
  333.         return;
  334.     }
  335.     var root = xmlDoc.documentElement;   //root node
  336.     var totalbytes = root.childNodes(0).text;
  337.     var uploadbytes = root.childNodes(1).text;
  338.     var percent = root.childNodes(2).text;
  339.     ProgressPercent.innerHTML = percent + "%";
  340.     ProgressBar.style.width = percent + "%";
  341.     uploadSize.innerHTML = uploadbytes;
  342.   uploadSpeed.innerHTML = root.childNodes(3).text;
  343.     totalTime.innerHTML = root.childNodes(4).text;
  344.     leftTime.innerHTML = root.childNodes(5).text;
  345.     if (percent<100){
  346.       setTimeout("DisplayProgressBar('" + xmlPath + "')",1000);
  347.     }
  348. }
  349. function displayProgress(){
  350.     var objProgress = document.getElementById("Progress");
  351.   objProgress.style.display = "";
  352. }
  353. function closeProgress(){
  354.     var objProgress = document.getElementById("Progress");
  355.   objProgress.style.display = "none";
  356. }
  357. function setProgressDivPos(){
  358.     var objProgress = document.getElementById("Progress");
  359.     objProgress.style.top = document.body.scrollTop+(document.body.clientHeight-document.getElementById("Progress").offsetHeight)/2
  360.     objProgress.style.left = document.body.scrollLeft+(document.body.clientWidth-document.getElementById("Progress").offsetWidth)/2;
  361. }
  362. </script>
  363. <style type="text/css">
  364. .progress {
  365.     position: absolute;
  366.     padding: 4px;
  367.     top: 50;
  368.     left: 400;
  369.     font-family: Verdana, Helvetica, Arial, sans-serif;
  370.     font-size: 12px;
  371.     width: 250px;
  372.     height:100px;
  373.     background: #FFFBD1;
  374.     color: #3D2C05;
  375.     border: 1px solid #715208;
  376.     /* Mozilla proprietary */
  377.     -moz-border-radius: 5px;
  378.     /*-moz-opacity: 0.95; */
  379.     text-align:left;
  380. }
  381. .progress table,.progress td{
  382.   font-size:9pt;
  383. }
  384. .Bar{
  385.   width:100%;
  386.     height:15px;
  387.     background-color:#CCCCCC;
  388.     border: 1px inset #666666;
  389.     margin-bottom:4px;
  390. }
  391. .ProgressPercent{
  392.     font-size: 9pt;
  393.     color: #000000;
  394.     height: 15px;
  395.     position: absolute;
  396.     z-index: 20;
  397.     width: 100%;
  398.     text-align: center;
  399. }
  400. .ProgressBar{
  401.   background-color:#91D65C;
  402.     width:1px;
  403.     height:15px;
  404. }
  405. </style>
  406. <div id="Progress" style="display:none;" class="progress">
  407.     <div class="bar">
  408.         <div id="ProgressPercent" class="ProgressPercent">0%</div>
  409.         <div id="ProgressBar" class="ProgressBar"></div>
  410.     </div>
  411.     <table border="0" cellspacing="0" cellpadding="2">
  412.         <tr>
  413.             <td>uploaded size</td>
  414.             <td>:</td>
  415.             <td id="uploadSize"></td>
  416.         </tr>
  417.         <tr>
  418.             <td>upload speed</td>
  419.             <td>:</td>
  420.             <td id="uploadSpeed"> </td>
  421.         </tr>
  422.         <tr>
  423.             <td>total time</td>
  424.             <td>:</td>
  425.             <td id="totalTime"> </td>
  426.         </tr>
  427.         <tr>
  428.             <td>remain time</td>
  429.             <td>:</td>
  430.             <td id="leftTime"> </td>
  431.         </tr>
  432.     </table>
  433. </div>

Example.asp代码如下:

 

 

  1. <%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
  2. <!--#include file="SundyUpload.asp"-->
  3. <%  
  4. '此例子文档编码都是UTF-8,如果是其他编码的系统,请将编码转换为相应的编码,不然表单获取数据可能会乱码  
  5. Dim objUpload,opt  
  6. Dim xmlPath  
  7. Dim fileFormName,objFile,counter  
  8. opt = request.QueryString("opt")  
  9. If opt = "Upload" Then    
  10.   xmlPath = Server.MapPath(request.QueryString("xmlPath"))'将虚拟路径转换为实际路径   
  11.   Set objUpload=new SundyUpload '建立上传对象      
  12.     objUpload.UploadInit xmlPath,"utf-8"      
  13.     counter = 1      
  14.     Response.Write("普通表单:" & objUpload.Form("normalForm") & "<BR><BR>")'获取表单数据      
  15.     For Each fileFormName In objUpload.objFile        
  16.       Set objFile=objUpload.objFile(fileFormName)          
  17.         fileSize = objFile.FileSize          
  18.         fileName = objFile.FileName          
  19.         If fileSize > 0 Then              
  20.           Response.Write("File Size:" & fileSize & "<BR>")              
  21.             Response.Write("File Name:" & objFile.FilePath & fileName & "<BR>")              
  22.             Response.Write("File Description:" & objUpload.Form("fileDesc" & counter) & "<BR><BR>")              
  23.             'objFile.SaveAs 文件的实际路径(可以自行处理)      
  24.     End If          
  25.         counter = counter + 1      
  26.     Next        
  27. End If  
  28. '为上载进度条数据文件(XML文件指定虚拟路径)  
  29. '最好是随机的,因为可能多个人同时上载,需要不同的进度数据  
  30. '这个路径需要在提交的时候传入上载组件中,以便在上载过程中更改进度数据  
  31. '客户端使用Javascript来读取此XML文件,显示进度  xmlPath = "upload/" & Timer & ".xml"  
  32. %>
  33. <html >
  34. <head>
  35. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  36. <title>Sundy Upload Progress Bar Example</title>
  37. <script language="javascript">  function chkFrm(){    var objFrm = document.frmUpload;      if (objFrm.file1.value=="" && objFrm.file2.value==""){        alert("请选择一个文件");          objFrm.file1.focus();          return false;      }      objFrm.action = "Example.asp?opt=Upload&xmlPath=<%=xmlPath%>";      startProgress('<%=xmlPath%>');//启动进度条      return true;  }  </script>
  38. </head>
  39. <body>
  40. <form name="frmUpload" method="post" action="Example.asp" enctype="multipart/form-data" onSubmit="return chkFrm()">
  41.   普通表单:<BR>
  42.   <input type="text" name="normalForm" size="40">
  43.   <BR>
  44.   <BR>
  45.   文件1:<BR>
  46.   <input type="file" name="file1" size="40">
  47.   </br>
  48.   <input type="text" name="fileDesc1" size="30">
  49.   <BR>
  50.   <BR>
  51.   文件2:<BR>
  52.   <input type="file" name="file2" size="40">
  53.   </br>
  54.   <input type="text" name="fileDesc2" size="30">
  55.   <BR>
  56.   <input type="submit" name="btnSubmit" value="submit"/>
  57. </form>
  58. </body>
  59. </html>

将以上代码存为相应的文件,记得这个例子的页面编码一定要是UTF-8的,以后需要自己可以改。 进度条图示(本地上载): 下载源码,例子

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值