XMLHTTP Upload file

Source: Internet
Author: User
Tags array base64 chr modify save file sendfile zip client
Writing ideas:Sends the local file to the destination after the client has passed the base64 encoding.
During the test, the upload file was too large, which caused the timeout to be unsuccessful.
It was later improved. Send the code segment. Test 20M Success

purpose of writing:In the traditional solution, one time to select the upload can be. But it's troublesome to read the file path in the database and upload it to a place.
If you get the path one to find with FTP of course is also OK, but every time I look for these files will be more time-consuming. This is done primarily to get files through the file path in the database. Upload files to one place at a time.
The main purpose is to exercise yourself.

resolution process:At first, try to use the analog keyboard input to forcibly assign the value to the file control to upload in the traditional way. However, the old encountered a null value of the situation, so many documents have not been sent out. Look up some information, now is to take all the path through the database and then write to a JS. Then in the foreground with JS read these paths, through the XMLHTTP to send files.
Because IE does not like XMLHTTP. Always think he has malicious behavior. So always talk about warning. Therefore, you cannot use the Web path when you operate. You can only access it using a physical path.
The server then has a file to receive the encoding and decode it. So I call it "C/S." ^_^

At present, a lot of code is still in perfect.
Brief introduction:

Aryfiles.push ("C:\\aaa.zip");
Aryfiles.push ("C:\\bbb.exe");
This is the file path and file. Available for multiple
This path can also be used by the file control on the client to access the

http://www.xxx.com/xxx/xxx.asp
This is the destination, you can change to the address you want.

Ado_stream. LoadFromFile (Server.MapPath (".") & "/" & + Str_filename)
Server.MapPath (".") & "/" & + Str_filename Here is the read file.
Server.MapPath (".") & "/" & Path and storage path consistent

Ado_stream. SaveToFile Server.MapPath (".") & "/" & str_filename,2
Server.MapPath (".") & "/" & This is the path for storing files. Str_filename as FileName

here the reading and storing are placed in the directory where the program is placed. You can keep it that way when you test it.

Place the first piece of code locally (eg:c:\upload.htm)

Put the second piece of code on the server, can be a local server, can be a public network server. Consistent with the destination above
(eg:http://www.xxx.com/upload.asp or http://localhost/www/upload.asp)


Action: Locate the first paragraph where the code is saved. Execute (eg: Open C-Disk to perform upload.htm)

At present, the procedure is still in the process of commissioning, I hope you put forward valuable comments

Client Code<ptml><pead></pead><body> <input Type=button value= "send"/> <input Type=button value= "Interrupt"/> <div id= "ddd" width=300px></div> <br></br> <DIVid=div_message></DIV> </body><script language=vbscript> Function bytes2bstr (vin) Strreturn = "" For i = 1 to LenB (vin) Thischarcode = AscB (MidB (vin,i,1)) Ifthischarcode < &h80 Then = Strreturn strreturn (&AMP;CHR) Else Thischarcode = as CB (MidB (vin,i+1,1)) Strreturn =strreturn & Chr (CLng (thischarcode) * &h100 +cint (nextcharcode)) i = i + 1 end If Ne XT Bytes2bstr = Strreturn Endfunction</script><script language=javascript> var xmlhttp; var Ado_stream; Varmfilename, Mpartstart, Mpartid, mpartend; var Sendcount; Varblocksize; var breaked; var aryfiles; BlockSize = 1024*100;//The number of bytes sent each time breaked = false; Aryfiles = new Array (); Start sending file function Beginsendfiles () {initaryfiles (); SendFile (Aryfiles.pop ());} Constructs an array function for the file to be sent Initaryfiles () {Aryfiles.push ("C:\\aaa.zip"); Aryfiles.push ("C:\\bbb.exe");//c:\\aaa.zip c:\\ Bbb.exe local file Aryfiles.reverse ();//filename} function SendFile (Vfullpath) {//empty file does not perform upload if (!vfullpath) {return;} breaked = false; div_message.innerhtml = ""; Ado_stream = new ActiveXObject ("ADODB. Stream "); The stream ado_stream to read the file. Type = 1; Ado_stream. Open (); Ado_stream. LoadFromFile (Vfullpath); Read file Ado_stream.position = 0; Sendcount = Math.ceil (ado_stream.size/blocksize); If there are more than one time to send a//alert (Sendcount); var reg =/\b\w+.\w+$/gi mfilename =reg.exec (Vfullpath); Mpartstart = true; Mpartid = 1; Mpartend =false; SendData (); The function SendData () {if (Sendcount > 0) {var dom = new ActiveXObject ("MSXML2. DOMDocument "); The sent XML file Dom.async = false; Dom.resolveexternals = false; Constructs the XML file header var node =dom.createprocessinginstruction ("xml", "version= ' 1.0 '");d om.appendchild (node); node = null; Constructs the root node var root =dom.createelement ("root"); Dom.appendchild (Root);d Om.documentElement.setAttRibute ("Xmlns:dt", "urn:schemas-microsoft-com:datatypes"); Constructs nodes that hold binary data updata node =dom.createelement ("Updata"); Node.datatype = "Bin.base64"; Bin Base64 encoded var att = dom.createattribute ("FileName"); FileName attribute att.value = mfilename; Node.setattributenode (ATT); ATT = null; Varatt = Dom.createattribute ("Partstart"); Segment start Mark Att.value =mpartstart; Node.setattributenode (ATT); ATT = null; var att =dom.createattribute ("PartID"); segmental serial number att.value = Mpartid; Node.setattributenode (ATT); ATT = null; var att =dom.createattribute ("partend"); Segmented end tag att.value = mpartend; Node.setattributenode (ATT); ATT = null; Root.appendchild (node); node.nodetypedvalue = Ado_stream. Read (BlockSize); Node data is read from stream, fixed length node = null; Sendcount-= 1; XMLHTTP = Newactivexobject ("Microsoft.XMLHTTP"); Xmlhttp.open ("POST", "http://www.xxx.com/xxx/xxx.asp", false);// Http://www.xxx.com/xxx/xxx.asp is the file xmlhttp.onreadystatechange=callback on the Web path; Xmlhttp.send (DOM); Mpartstart = false; XMLHTTP = null; }else {Ado_stream. Close (); Ado_stream = null; The function CallBack () {//Upload success if (xmlhttp.readystate = 4) {//check whether to interrupt upload if (breaked) {return;} if (Sendcount > 0) { Mpartid + 1; div_message.innerhtml + = ("<br>" + XMLHTTP. ResponseText); var p= math.floor (Mpartid/(Math.ceil (ado_stream.size/blocksize) + 1)) *100); Calculate Progress Percentage Showbar (P); var t = settimeout ("SendData ();", 1); }else {///transmit the file//div_message.innerhtml + + + "Transfer finished!" <br> "; Continue to pass the next document Showbar (0); var cFile = Aryfiles.pop (); SendFile (CFile); }} function Showbar (per) {//progress bar ddd.innerhtml= "<table width= ' border=0 cellpadding= ' 0 ' cellspacing= ' 0 ' >&lt TR&GT;&LT;TD bgcolor= ' #6699FF ' ><input type=button style= ' width: "+ per +"%; border:0px; Background: #005599; Color: #FFFFFF ' value= ' + per + "%> </td></tr></table>"; }</script></ptml> [Ctrl + A ALL SELECT hint: You can modify some of the code, and then run]


Service-side code<%@ language=vbscript%> <% Option Explicit response.expires = 0 ' defines variables and objects. Dim Ado_stream Dim Xml_dom Dim Xml_data Dim Str_filenamedim Bol_partstartdim Int_partiddim ' Create stream object Set Ado_stream = Server.CreateObject ("ADODB"). Stream ")" creates the Xmldom object Set xml_dom = Server.CreateObject ("MSXML2) from the Request object. DOMDocument ") xml_dom.load (request) reads the node set xml_data = Xml_dom.selectsinglenode (" Root/updata ") that contains binary data 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")) ' opens the stream object, storing the data in Ado_stream. Type = 1 ' 1=adtypebinary ado_stream.open if not Bol_partstart then Ado_stream. LoadFromFile (Server.MapPath (".") & "/" & + str_filename) ' Read file Ado_stream.position = Ado_stream.size End Ifado _stream. Write xml_data.nodetypedvalue ' file to disk Ado_stream. SaveToFile Server.MapPath (".") & "/" & str_filename,2 ' save file 2=adsAvecreateoverwrite ado_stream.close ' Release resource set ado_stream = Nothing Set xml_dom = Nothing ' returns information to browser Response.Write ' UPL Oad successful! " & Str_filename & Int_partid & bol_partstart%>

[Ctrl + A ALL SELECT hint: You can modify some of the code, and then run]

In addition: The purpose of this article is not to say that this program can solve the problem for friends, the main purpose is to communicate with you some experience:
Encounter problems-> find problems-> solve problems.
I hope everyone will not become a standard code robot.
Learn more, see more, think more, practise more.
Nothing is impossible to solve.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.