mozilla

Compare Revisions

Uploading and Downloading Files

Change Revisions

Revision 69482:

Revision 69482 by Nickolay on

Revision 69483:

Revision 69483 by andpol on

Title:
Uploading and Downloading Files
Uploading and Downloading Files
Slug:
FileGuide/FileUpDown
FileGuide/FileUpDown
Content:

Revision 69482
Revision 69483
n13    <h4 name="HTTP_File_Upload">n13    <h4 id="HTTP_File_Upload" name="HTTP_File_Upload">
nn89    <p>
90      <br>
91      <strong>Example HTTP POST for multipart/form-data (Using XP
 >COM syntax)</strong>
92    </p>
93    <pre class="eval">
94//Buffer the upload file
95var inStream = Cc["@mozilla.org/network/file-input-stream;1"].cre
 >ateInstance(Ci.nsIFileInputStream);
96inStream.init(nsIFile, 1, 1, inStream.CLOSE_ON_EOF);
97var bufInStream = Cc["@mozilla.org/network/buffered-input-stream;
 >1"].createInstance(Ci.nsIBufferedInputStream);
98bufInStream.init(inStream, 4096);
99 
100//Setup the start Boundery stream
101var boundary = "X-------------X" + Math.random();
102var startBoundaryStream = Cc["@mozilla.org/io/string-input-stream
 >;1"].createInstance(Ci.nsIStringInputStream);
103startBoundaryStream.setData("\r\n--"+boundary+"\r\n", -1);
104 
105//Setup the middle Boundary stream
106var boundaryStream = Cc["@mozilla.org/io/string-input-stream;1"].
 >createInstance(Ci.nsIStringInputStream);
107boundaryStream.setData("\r\n--"+boundary+"\r\n", -1);
108 
109//Setup the end Boundery stream
110var endBoundaryStream = Cc["@mozilla.org/io/string-input-stream;1
 >"].createInstance(Ci.nsIStringInputStream);
111endBoundaryStream.setData("\r\n--"+boundary+"--", -1);
112 
113//Setup the binary data (file upload) mime-stream
114var file_mimeStream = Cc["@mozilla.org/network/mime-input-stream;
 >1"].createInstance(Ci.nsIMIMEInputStream);
115file_mimeStream.addContentLength = false;
116file_mimeStream.addHeader("Content-disposition","form-data; name=
 >\"aValue\"; filename=\""+nsIFile.leafName+"\"");
117file_mimeStream.addHeader("Content-type","application/octet-strea
 >m;");
118file_mimeStream.setData(bufInStream);
119 
120//Setup the "user" mime-stream
121var user_mimeStream = Cc["@mozilla.org/network/mime-input-stream;
 >1"].createInstance(Ci.nsIMIMEInputStream);
122user_mimeStream.addContentLength = false;
123user_mimeStream.addHeader("Content-disposition", "form-data; name
 >=\"user\"");
124var strStream = Cc["@mozilla.org/io/string-input-stream;1"].creat
 >eInstance(Ci.nsIStringInputStream);
125strStream.setData("SteveJobs", -1);
126user_mimeStream.setData(strStream);
127 
128//Setup the multiplex stream to combine the necessary streams
129var multiStream = Cc["@mozilla.org/io/multiplex-input-stream;1"].
 >createInstance(Ci.nsIMultiplexInputStream);
130multiStream.appendStream(startBoundaryStream);
131multiStream.appendStream(file_mimeStream);
132multiStream.appendStream(boundaryStream);
133multiStream.appendStream(user_mimeStream);
134multiStream.appendStream(endBoundaryStream);
135 
136//Setup the POST request
137var req = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createIns
 >tance(Ci.nsIXMLHttpRequest);
138req.open("POST", URL_VALUE, false);  //false makes this an asyncr
 >onous request
139req.setRequestHeader("Content-type", "multipart/form-data; bounda
 >ry="+boundary);
140req.setRequestHeader("Content-length", multiStream.available());
141 
142//Send the request (finally!)
143req.send(multiStream); 
144 
145</pre>
89    <h4 name="HTTP_PUT">146    <h4 id="HTTP_PUT" name="HTTP_PUT">
t111    <h4 name="Downloading_Files">t168    <h4 id="Downloading_Files" name="Downloading_Files">

Back to History