Created
September 17, 2011 00:07
-
-
Save bgreenlee/1223431 to your computer and use it in GitHub Desktop.
Multipart Binary POSTs in Javascript #javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var dataURL = this.canvas.toDataURL(this.getImageType()); // grab the snapshot as base64 | |
var imgData = atob(dataURL.substring(13 + this.getImageType().length)); // convert to binary | |
var filenameTimestamp = (new Date().getTime()); | |
var separator = "----------12345-multipart-boundary-" + filenameTimestamp; | |
// Javascript munges binary data when it undergoes string operations (such as concatenation), so we need | |
// to jump through a bunch of hoops with streams to make sure that doesn't happen | |
// create a string input stream with the form preamble | |
var prefixStringInputStream = Components.classes["@mozilla.org/io/string-input-stream;1"].createInstance(Components.interfaces.nsIStringInputStream); | |
var formData = | |
"--" + separator + "\\r\\n" + | |
"Content-Disposition: form-data; name=\"data\"; filename=\"snapshot_" + filenameTimestamp + | |
(this.getImageType() === "image/jpeg" ? ".jpg" : ".png") + "\"\\r\\n" + | |
"Content-Type: " + this.getImageType() + "\\r\\n\\r\\n"; | |
prefixStringInputStream.setData(formData, formData.length); | |
// write the image data via a binary output stream, to a storage stream | |
var binaryOutputStream = Components.classes["@mozilla.org/binaryoutputstream;1"].createInstance(Components.interfaces.nsIBinaryOutputStream); | |
var storageStream = Components.classes["@mozilla.org/storagestream;1"].createInstance(Components.interfaces.nsIStorageStream); | |
storageStream.init(4096, imgData.length, null); | |
binaryOutputStream.setOutputStream(storageStream.getOutputStream(0)); | |
binaryOutputStream.writeBytes(imgData, imgData.length); | |
binaryOutputStream.close(); | |
// write out the rest of the form to another string input stream | |
var suffixStringInputStream = Components.classes["@mozilla.org/io/string-input-stream;1"].createInstance(Components.interfaces.nsIStringInputStream); | |
formData = | |
"\\r\\n--" + separator + "\\r\\n" + | |
"Content-Disposition: form-data; name=\"description\"\\r\\n\\r\\n" + description + "\\r\\n" + | |
"--" + separator + "--\\r\\n"; | |
suffixStringInputStream.setData(formData, formData.length); | |
// multiplex the streams together | |
var multiStream = Components.classes["@mozilla.org/io/multiplex-input-stream;1"].createInstance(Components.interfaces.nsIMultiplexInputStream); | |
multiStream.appendStream(prefixStringInputStream); | |
multiStream.appendStream(storageStream.newInputStream(0)); | |
multiStream.appendStream(suffixStringInputStream); | |
// post it | |
req.open("POST", "http://yoursite.com/upload_endpoint", true); | |
req.setRequestHeader("Accept", "*/*, application/xml"); | |
req.setRequestHeader("Content-type", "multipart/form-data; boundary=" + separator); | |
req.setRequestHeader("Content-length", multiStream.available()); | |
req.setRequestHeader("Authorization", "Basic " + btoa(username + ":" + password)); | |
req.setRequestHeader("User-Agent", "YourUserAgent/1.0.0"); | |
req.send(multiStream); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment