ASP 上传进度完美解决_无刷新

2008-01-16 16:06:41

ASP 上传进度完美解决 无刷新显示进度 1.上传的代码,网上到处是,此处仅测试,文件未被上传至服务器,文件名:up.asp [code]<% Dim biData, PostData, TotalBytes, ChunkBytes Dim Percent ChunkBytes = 20 * 1024 ' 分块大小为1K TotalBytes = Request.TotalBytes ' 总大小 PostData = "" ' 转化为文本类型后的数据 ReadedBytes = 0 ' 初始化为0 Set bSourceData = cr&#101;ateobject("ADODB.Stream") bSourceData.Type = 1 'Binary bSourceData.Mode=3 bSourceData.Open Do While ReadedBytes < TotalBytes biData = Request.BinaryRead(ChunkBytes) ' 当前块 PostData = PostData & BinaryToString(biData,ChunkBytes) ' 将当前块转化为文本并拼接 bSourceData.Write biData ' 直接使用write方法将当前文件流写入bSourceData中 ReadedBytes = ReadedBytes + ChunkBytes ' 记录已读大小 If ReadedBytes > TotalBytes Then ReadedBytes = TotalBytes Percent = ReadedBytes/TotalBytes*100 '关键点,将进度存入 Application Application("Percent") =Percent Loop ' 借助RecordSet将二进制流转化成文本 Function BinaryToString(biData,Size) Const adLongVarChar = 201 Set RS = CreateObject("ADODB.Recordset") RS.Fields.Append "mBinary", adLongVarChar, Size RS.Open RS.AddNew RS("mBinary").AppendChunk(biData) RS.Update BinaryToString = RS("mBinary").Value RS.Close End Function [/code] 2.显示进度程序,生成XML进度数据 ,文件名 progress.asp [code] <%@EnableSessionState=False%> <% Response.CacheControl = "no-cache" Response.Expires = -1 %> <% Dim xmlDoc Set xmlDoc = Server.CreateObject("Microsoft.XMLDOM") xmlDoc.loadXML "<ProgressInfo/>" Set root = xmlDoc.documentElement Set elePercent = xmlDoc.cr&#101;ateElement("Percent") root.appendChild(elePercent) If Application("Percent")>=100 Then '加载完后,自动清零,为下一次加载做准备 Application("Percent")=0 elePercent.text ="100" Else elePercent.text = Application("Percent") End If Response.Write("<?xml version=""1.0"" encoding=""GB2312""?>"&xmlDoc.xml) %> [/code] 3.通过JS的 XMLDOM读取 progress.asp,取得进度数据,并显示到前台 [code] <html> <head> <script language="JavaScript"> var _xmlDom = null; var arrXmlDomTypes = ['MSXML2.DOMDocument.6.0','MSXML2.DOMDocument.3.0','Microsoft.XMLDOM']; for(var i = 0;i<arrXmlDomTypes.length;i++){ try{ _xmlDom = new ActiveXObject(arrXmlDomTypes[i]); }catch(ex){}//不支持MSXML.XMLDOM对象的IE } //取得进度 function getProgress(){ var url = "progress.asp"; if(_xmlDom==null){ alert("创建 XMLDom 失败."); return; } _xmlDom.async = true; _xmlDom.onreadystatechange = Function( "loadComplete();" ); _xmlDom.load( url ); } function loadComplete(){ var iReadyState = _xmlDom.readyState; if(_xmlDom.xml==""){ return; } if(iReadyState!=4){ return; } var oRoot = _xmlDom.documentElement; if(oRoot!=null){ var percent = oRoot.childNodes(0).text; trace("percent=="+percent); //这里就是得到的进度了,想怎么处理就怎么处理了,也可以通过 JS,将其传给FLASH,作个FLASH上传显示进度也没有问题了,嘿嘿 if(percent>=100){ clearTimeout(iTimerID); }else{ iTimerID = setTimeout("getProgress()", 100); } trace("percent=="+percent); } } function upload(){ var host = "http://zhangxi:28/test/mutil/up.asp"; document.myform.action = host; document.myform.target="upload"; document.myform.submit(); //提交表单; getProgress(); } function trace(arg){ document.getElementById("label").innerHTML += arg+"<br />"; } </script> </head> <body> <form name="myform" method="post" action="up.asp" enctype="multipart/form-data" target="upload"> <input type="file" name="filefield1">(请上传小于10MB的文件)<br> <input type="button" value="上传" onClick="upload()"> </form> <div id="label"></div> </body> </html> [/code] [url=http://www.shareme.cn/upload/upload.htm]演示[/url] 前台代码有所改动