Writing idea: Encode the local file with base64 on the client and then send it to the destination.
During the test, the uploaded file was too large, causing a timeout and failure.
Later, it was improved. The encoding was sent in segments. The test of 20M was successful.
Purpose of writing: In the traditional solution, you can select and upload one at a time. However, it is more troublesome when it comes to reading the file paths in the database and uploading these files to one place.
It is of course possible to use ftp to find the paths one by one, but it will be more time-consuming for me to find these files every time. The purpose of writing this here is mainly to obtain files through the file path in the database and upload files to one place in batches at a time.
The main purpose is to train yourself.
Solution process: At first, I tried to use simulated keyboard input to forcefully assign values to the file control and upload it using the traditional method. But I always encounter null values, and even many files are not sent out. After consulting some information, now I am fetching all the paths through the database and writing them into a js. Then use js in the front desk to read these paths and send the file through xmlhttp.
Because Internet Explorer doesn't like xmlHttp very much and always thinks it has malicious behavior, so it always gives warnings. Therefore, you cannot use the web path during operation. You can only use the physical path to access it.
Then the server has a file to receive these encodings and decode them. So I call it c/s. ^_^
At present, many codes are still being improved.
A brief introduction:
-------------------------------------------------- ----------
aryFiles.push(c://aaa.zip);
aryFiles.push(c://bbb.exe);
Here is the file path and file. It can be multiple
In the future, this path can also be obtained on the client through the file control.
http://www.xxx.com/xxx/xxx.asp
This is the destination, you can change it to the address you want.
ado_stream.LoadFromFile(server.mappath(.) &/& + str_filename)
server.mappath(.) &/& + str_filename Here is the file to read.
server.mappath(.) &/& The path is consistent with the storage path
ado_stream.SaveToFile server.mappath(.) &/& str_filename,2
server.mappath(.) &/& This is the path to store the file. str_filename is the file name
The reading and storage here are placed in the directory where the program is placed. You can also keep it like this when testing.
Put the first piece of code locally (eg:c:/upload.htm)
Put the second piece of code on the server, which can be a local server or a public server. Keep it consistent with the destination above.
(eg:http://www.xxx.com/upload.asp or http:// localhost/www/upload.asp)
-------------------------------------------------- ---------------
Action: Find where the first piece of code is saved. Just execute it (eg: open the c drive and execute upload.htm)
Client code</P><P><html><head></head><body> <input type=button onclick=BeginSendFiles(); value=Send/> <input type=button onclick=JavaScript: Breaked= true; value=interrupt/> <div id=ddd width=300px></div> </br> <DIV id=div_message></DIV></body></P><P><script language=VBScript>Function bytes2BSTR(vIn) strReturn = For i = 1 To LenB(vIn) ThisCharCode = AscB(MidB(vIn,i ,1)) If ThisCharCode < &H80 Then strReturn = strReturn & Chr(ThisCharCode) Else NextCharCode = AscB(MidB(vIn,i+1,1)) strReturn = strReturn & Chr(CLng(ThisCharCode) * &H100 + CInt(NextCharCode)) i = i + 1 End If Next bytes2BSTR = strReturnEnd Function</script></P ><P><script language=javascript> var xmlhttp; var ado_stream; var mFileName, mPartStart, mPartID, mPartEnd; var SendCount; var BlockSize; var Breaked; var aryFiles;</P><P> BlockSize = 1024*100; //The number of bytes sent each timeBreaked = false; aryFiles = new Array();</P> P><P> // Start sending files function BeginSendFiles() { initAryFiles(); SendFile(aryFiles.pop()); } // Construct an array of files to be sent function initAryFiles() { aryFiles.push(c://aaa.zip) ; aryFiles.push(c://bbb.exe) ;//c://aaa.zip c ://bbb.exe local file aryFiles.reverse() ;//File name}</P><P> function SendFile(vFullPath) { // Empty file will not be uploaded if (!vFullPath) { return ; } Breaked = false ; div_message.innerHTML = ; ado_stream = new ActiveXObject(ADODB.Stream); // Read the file stream ado_stream.Type = 1; ado_stream.Open(); ado_stream.LoadFromFile( vFullPath); // Read the file ado_stream.position = 0;</P><P> SendCount = Math.ceil(ado_stream.size/BlockSize) ; // If there is a remainder, send one more time</P><P> // alert(SendCount) ;</P><P> var reg = //b/w+. /w+$/gi mFileName = reg.exec(vFullPath) ; mPartStart = true ; mPartID = 1 ; mPartEnd = false ; SendData() ; }</P><P> function SendData() { if (SendCount > 0) { var dom = new ActiveXObject(msxml2.DOMDocument); // Send xml file dom.async = false; dom.resolveExternals = false;</P><P> // Construct xml file header var node = dom.createProcessingInstruction(xml,version='1.0'); dom.appendChild(node); node = null; // Construct the root node var root = dom.createElement(root); dom.appendChild(root); dom.documentElement.setAttribute(xmlns:dt, urn:schemas-microsoft-com:datatypes); // Construct the node updata to save binary data node = dom.createElement(upData); node.dataType = bin.base64; // bin. base64 encoding var att = dom.createAttribute(FileName); // File name attribute att.value = mFileName; node.setAttributeNode(att); att = null; var att = dom.createAttribute(PartStart); // Section start mark att.value = mPartStart; node.setAttributeNode(att); att = null; var att = dom.createAttribute(PartID); // Section number att.value = mPartID; node.setAttributeNode(att); att = null; var att = dom.createAttribute(PartEnd); // Section end mark att.value = mPartEnd; node.setAttributeNode(att); att = null ;</P><P> root.appendChild(node) ; node.nodeTypedValue = ado_stream.Read(BlockSize); // Node data is read from stream, fixed length node = null; SendCount -= 1; xmlhttp = new ActiveXObject(Microsoft.XMLHTTP); xmlhttp.open(POST,http://www.xxx.com/xxx/xxx.asp, false); //http://www.xxx.com/xxx/xxx.asp is the file on the web path xmlhttp.onreadystatechange= CallBack; xmlhttp.send(dom); mPartStart = false; xmlhttp = null ; } else { ado_stream.Close(); ado_stream = null ; } } function CallBack() { // Upload successful if(xmlhttp.readystate == 4) { // Check whether the upload is interrupted if(Breaked) { return ; }</P><P> if (SendCount > 0) { mPartID += 1; // div_message.innerHTML += ( + xmlhttp.ResponseText) ; var p = Math.floor((mPartID/(Math.ceil(ado_stream.size/BlockSize) + 1)) * 100) ; // Calculate progress percentageShowBar(p) ; var t = setTimeout(SendData ();, 1) ; } else { // Completed file transfer //div_message.innerHTML += mFileName + Transfer completed! ;</P><P> // Continue to pass the next file ShowBar(0) ; var cFile = aryFiles.pop() ; SendFile(cFile) ; } } </P><P> }</P><P > function ShowBar(per) { // Progress bar ddd.innerHTML = <table width='200' border=0 cellpadding='0' cellspacing='0' ><tr><td bgcolor='#6699FF'><input type=button style=' width: + per + % ; border:0px; background:#005599; color:#FFFFFF' value= + per + %> </td></tr ></table> ; }</P><P></script></html></P><P>
server side
</P><P><%@ LANGUAGE=VBScript%><% Option ExplicitResponse.Expires = 0 </P><P>' Define variables and objects. dim ado_streamdim xml_domdim xml_datadim str_filenamedim bol_PartStartdim int_PartIDdim bol_PartEnd </P><P>' Create Stream object set ado_stream = Server.CreateObject(ADODB.Stream)' Create XMLDOM object from Request object set xml_dom = Server.CreateObject(MSXML2.DOMDocument)xml_dom.load(request)' Read out the node containing binary data set xml_data = xml_dom.selectSingleNode(root/upData)str_filename = xml_data.getAttribute(FileName)bol_PartStart = CBool(xml_data.getAttribute(PartStart ))int_PartID = CInt(xml_data.getAttribute(PartID))bol_PartEnd = CBool(xml_data.getAttribute(PartEnd))</P><P>' Open the Stream object and store the data in it ado_stream.Type = 1 ' 1=adTypeBinary ado_stream.open if not bol_PartStart then ado_stream.LoadFromFile(server.mappath(.) &/& + str_filename) ' Read the file ado_stream.position = ado_stream.sizeend ifado_stream.Write xml_data.nodeTypedValue' File save ado_stream.SaveToFile server.mappath(.) &/& str_filename,2'Save file 2=adSaveCreateOverWrite ado_stream.close </P><P> 'Release resources set ado_stream = Nothing set xml_dom = Nothing' Return information to the browser Response.Write Upload successful!& str_filename & int_PartID & bol_PartStart%> </P><P>