Skip to content

Instantly share code, notes, and snippets.

@mrdoob
Created January 10, 2012 21:47
Show Gist options
  • Save mrdoob/1591401 to your computer and use it in GitHub Desktop.
Save mrdoob/1591401 to your computer and use it in GitHub Desktop.
var MultipartForm = function () {
var boundary = '------------------------------' + Date.now(), string = '--' + boundary + '\r\n';
this.addVariable = function ( name, value ) {
string += 'Content-Disposition: form-data; name="' + name + '"\r\n';
string += 'Content-Type: text/plain\r\n';
string += '\r\n' + value + '\r\n';
string += '--' + boundary + '\r\n';
};
this.addFile = function ( name, filename, filetype, data ) {
string += 'Content-Disposition: form-data; name="' + name + '"; filename="' + filename + '"\r\n';
string += 'Content-Type: ' + filetype + '\r\n';
string += '\r\n' + data + '\r\n';
string += '--' + boundary + '\r\n';
};
this.getBoundary = function () {
return boundary;
};
this.getString = function () {
return string;
};
this.getBuffer = function () {
return new Uint8Array( Array.prototype.map.call( string, function ( x ) { return x.charCodeAt( 0 ) & 0xff; } ) ).buffer;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment