利用Xmlhttp 分块上传文件

日期:2004年10月7日 作者:清风网络学院



编写思路:把本地文件在客户端通过base64编码以后发送目的地.
测试过程中,上传文件过大,导致超时不成功.
后来经过改善.把编码分段发送.测试20M成功

编写目的:在传统的解决方案里面,一次一次选取上传可以.但是在碰到把数据库里文件路径读出来,并把这些文件上传到一个地方的时候就比较麻烦.
如果得到路径一个一个去找到用ftp当然也是可以的,但每次找这些文件我看都会比较费时。这里编写这个主要就是为了通过数据库里的文件路径取得文件.把文件一次批量上传到一个地方.
其主要目的还是为了锻炼一下自己.

解决过程:起初试着用模拟键盘输入强行赋值给file控件用传统的方法上传。可是老碰到空值的情况,以至有很多文件没有发送出去。查阅一些资料,现在是把所有路径通过数据库取到然后写到一个js里。然后在前台用js读取这些路径,通过xmlhttp来发送文件。
因为ie不太喜欢xmlHttp.总认为他有恶意行为.所以老谈出提示警告.所以操作的时候不能使用web路径.只能用物理路径去访问它.
然后服务端有一文件来接收这些编码,并对其进行解码.所以我称之为"c/s". ^_^

目前很多代码还在完善中.
简单介绍一下:

aryFiles.push("c:\\aaa.zip") ;
aryFiles.push("c:\\bbb.exe") ;

这里为文件路径和文件.可为多个
以后这个路径也可以通过file控件在客户端取到

http:// www.xxx.com/xxx/xxx.asp
这个是目的地,可以改为自己想要的地址.

ado_stream.LoadFromFile(server.mappath(".") &"/"& + str_filename)
server.mappath(".") &"/"& + str_filename 这里是读取文件.
server.mappath(".") &"/"& 路径和存放路径一致

ado_stream.SaveToFile server.mappath(".") &"/"& str_filename,2
server.mappath(".") &"/"& 这个为存放文件的路径. str_filename 为文件名


这里读取和存放都是放在程序所放目录中.大家测试的时候也可以保持这样即可

把第一段代码放到本地(eg:c:\upload.htm)

把第二段代码放到服务器上,可以是本地服务器,可以是公网服务器.和上面的目的地保持一致
(eg:http://www.xxx.com/upload.asp or http:// localhost/www/upload.asp)


操作:找到第一段代码保存的地方。执行即可(eg:打开c盘执行upload.htm)

客户段
 1 None.gif < html >
 2 None.gif < head >
 3 None.gif </ head >
 4 None.gif < body >   < input type = button onclick = " BeginSendFiles(); "
 5 None.gifvalue = " 发送 "   />   < input type = button onclick = " JavaScript:
 6 None.gif Breaked = true ; "  value= " 中断 "  /> <div id= " ddd "
 7 None.gif width = 300px ></ div >   < br ></ br >   < DIV
 8 None.gifid = div_message ></ DIV >
 9 None.gif </ body >
10 None.gif < script language = VBScript >  Function bytes2BSTR(vIn) strReturn  =
11 None.gif ""  For i  =   1  To LenB(vIn) ThisCharCode  =  AscB(MidB(vIn,i, 1 )) If
12 None.gifThisCharCode  <   & H80 Then strReturn  =  strReturn  &
13 None.gifChr(ThisCharCode) Else NextCharCode  =  AscB(MidB(vIn,i + 1 , 1 )) strReturn  =
14 None.gifstrReturn  &  Chr(CLng(ThisCharCode)  *   & H100  +
15 None.gifCInt(NextCharCode)) i  =  i  +   1  End If Next bytes2BSTR  =  strReturn End
16 None.gifFunction
17 None.gif </ script >
18 None.gif < script language = javascript >  var xmlhttp ; var ado_stream ; var
19 None.gifmFileName, mPartStart, mPartID, mPartEnd ; var SendCount ; var
20 None.gifBlockSize ; var Breaked ; var aryFiles ; BlockSize  =   1024 * 100
21 None.gif; // 每次发送字节数 Breaked = false ; aryFiles = new Array() ;  //  开始发送文件
22 ExpandedBlockStart.gifContractedBlock.gif function BeginSendFiles()  dot.gif { initAryFiles() ; SendFile(aryFiles.pop()) ;
23ExpandedBlockEnd.gif}
  //  构造待发送文件的数组 function initAryFiles() { aryFiles.push("c:\\aaa.zip")
24 None.gif ; aryFiles.push( " c:\\bbb.exe " ) ;  // c:\\aaa.zip c:\\bbb.exe本地文件
25 None.gif aryFiles.reverse() ; // 文件名 } function SendFile(vFullPath) {  //  空文件则不执行上传
26 ExpandedBlockStart.gifContractedBlock.gif if  ( ! vFullPath)  dot.gif return ; }  Breaked  =   false  ; div_message.innerHTML  =
27 None.gif ""  ; ado_stream  =   new  ActiveXObject( " ADODB.Stream " );  //  读取文件的流
28 None.gif ado_stream.Type  =   1 ; ado_stream.Open();
29 None.gifado_stream.LoadFromFile(vFullPath);  //  读取文件 ado_stream.position = 0 ;
30 None.gif SendCount  =  Math.ceil(ado_stream.size / BlockSize) ;  //  如果有余数则多发送一次  //
31 None.gif alert(SendCount) ; var reg  =   / \b\w + .\w + $ / gi mFileName  =
32 None.gifreg.exec(vFullPath) ; mPartStart  =   true  ; mPartID  =   1  ; mPartEnd  =
33 ExpandedBlockStart.gifContractedBlock.gif false  ; SendData() ; } function SendData()  dot.gif if (SendCount > 0dot.gif{
34InBlock.gifvar dom = new ActiveXObject("msxml2.DOMDocument"); // 发送的xml文件
35InBlock.gifdom.async = false; dom.resolveExternals = false// 构造xml文件头 var node =
36InBlock.gifdom.createProcessingInstruction("xml","version='1.0'");
37InBlock.gifdom.appendChild(node) ; node = null ; // 构造root节点 var root =
38InBlock.gifdom.createElement("root"); dom.appendChild(root) ;
39InBlock.gifdom.documentElement.setAttribute("xmlns:dt",
40InBlock.gif"urn:schemas-microsoft-com:datatypes"); // 构造保存二进制数据的节点updata node =
41InBlock.gifdom.createElement("upData") ; node.dataType = "bin.base64" ; //
42InBlock.gifbin。base64编码 var att = dom.createAttribute("FileName") ; // 文件名属性
43InBlock.gifatt.value = mFileName ; node.setAttributeNode(att) ; att = null ; var
44InBlock.gifatt = dom.createAttribute("PartStart") ; // 分段开始标记 att.value =
45InBlock.gifmPartStart ; node.setAttributeNode(att) ; att = null ; var att =
46InBlock.gifdom.createAttribute("PartID") ; // 分段序号 att.value = mPartID ;
47InBlock.gifnode.setAttributeNode(att) ; att = null ; var att =
48InBlock.gifdom.createAttribute("PartEnd") ; // 分段结束标记 att.value = mPartEnd ;
49InBlock.gifnode.setAttributeNode(att) ; att = null ; root.appendChild(node) ;
50InBlock.gifnode.nodeTypedValue = ado_stream.Read(BlockSize); // 节点数据从stream读取,固定长度
51InBlock.gifnode = null ; SendCount -= 1 ; xmlhttp = new
52InBlock.gifActiveXObject("Microsoft.XMLHTTP");
53InBlock.gifxmlhttp.open("POST","http://www.xxx.com/xxx/xxx.asp"false);
54InBlock.gif//http://www.xxx.com/xxx/xxx.asp 为web路径上的文件 xmlhttp.onreadystatechange=
55ExpandedSubBlockEnd.gifCallBack ; xmlhttp.send(dom); mPartStart = false ; xmlhttp = null ; }

56ExpandedSubBlockStart.gifContractedSubBlock.gifelse dot.gif{ ado_stream.Close(); ado_stream = null ; } }
 function CallBack()
57 ExpandedBlockStart.gifContractedBlock.gif dot.gif // 上传成功 if(xmlhttp.readystate == 4) { // 检查是否中断上传 if(Breaked) {
58ExpandedBlockStart.gifContractedBlock.gifreturn ; }
  if  (SendCount  >   0 dot.gif { mPartID += 1 ; //
59InBlock.gifdiv_message.innerHTML += ("<br>" + xmlhttp.ResponseText) ; var p
60InBlock.gif= Math.floor((mPartID/(Math.ceil(ado_stream.size/BlockSize) + 1)) *
61InBlock.gif100) ; // 计算进度百分比 ShowBar(p) ; var t = setTimeout("SendData();", 1) ; }
62ExpandedSubBlockStart.gifContractedSubBlock.gifelse dot.gif// 传送完文件 //div_message.innerHTML += mFileName +
63InBlock.gif"传送完毕!<br>" ; // 继续传下一文件 ShowBar(0) ; var cFile = aryFiles.pop()
64ExpandedBlockStart.gifContractedBlock.gif; SendFile(cFile) ; }
 }
 } function ShowBar(per)  dot.gif // 进度条 ddd.innerHTML
65InBlock.gif= "<table width='200' border=0 cellpadding='0' cellspacing='0'
66InBlock.gif><tr><td bgcolor='#6699FF'><input type=button style='
67InBlock.gifwidth:" + per + "% ; border:0px; background:#005599; color:#FFFFFF'
68InBlock.gifvalue=" + per + "%> </td></tr></table>" ; }
69InBlock.gif</script>
70InBlock.gif</html>

服务器端
 1 None.gif <% @ LANGUAGE = VBScript %>  
 2 None.gif <%  Option Explicit 
 3 None.gifResponse.Expires  =   0   
 4 None.gif
 5 None.gif '  定义变量和对象。 
 6 None.gif dim ado_stream 
 7 None.gifdim xml_dom 
 8 None.gifdim xml_data 
 9 None.gifdim str_filename
10 None.gifdim bol_PartStart
11 None.gifdim int_PartID
12 None.gifdim bol_PartEnd 
13 None.gif
14 None.gif '  创建 Stream 对象 
15 None.gif set  ado_stream  =  Server.CreateObject( " ADODB.Stream "
16 None.gif '  从Request对象创建 XMLDOM对象 
17 None.gif set  xml_dom  =  Server.CreateObject( " MSXML2.DOMDocument "
18 None.gifxml_dom.load(request) 
19 None.gif '  读出包含二进制数据的节点 
20 None.gif set  xml_data  =  xml_dom.selectSingleNode( " root/upData "
21 None.gifstr_filename  =  xml_data.getAttribute( " FileName " )
22 None.gifbol_PartStart  =  CBool(xml_data.getAttribute( " PartStart " ))
23 None.gifint_PartID  =  CInt(xml_data.getAttribute( " PartID " ))
24 None.gifbol_PartEnd  =  CBool(xml_data.getAttribute( " PartEnd " ))
25 None.gif
26 None.gif '  打开Stream对象,把数据存入其中  
27 None.gif ado_stream.Type  =   1   '  1=adTypeBinary  
28 None.gif ado_stream.open  
29 None.gif if  not bol_PartStart then
30 None.gif  ado_stream.LoadFromFile(server.mappath( " . " & " / " &   +  str_filename)      '  读取文件
31 None.gif   ado_stream.position  =  ado_stream.size 
32 None.gifend  if
33 None.gifado_stream.Write xml_data.nodeTypedValue 
34 None.gif '  文件存盘 
35 None.gif ado_stream.SaveToFile server.mappath( " . " & " / " &  str_filename, 2  
36 None.gif ' 保存文件 2=adSaveCreateOverWrite  
37 None.gif ado_stream.close 
38 None.gif
39 None.gif '  释放资源 
40 None.gif set  ado_stream  =  Nothing  
41 None.gif set  xml_dom  =  Nothing 
42 None.gif '  向浏览器返回信息 
43 None.gif Response.Write  " Upload successful! " &  str_filename  &  int_PartID  &  bol_PartStart
44 None.gif %>  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值