Hi all,
I have an array buffer, I obtained it from: 1) draw image to canvas 2) canvas.toBlob 3) FileReader.readAsArrayBuffer
This is my code:
postData = encodeFormData({
'image_url': 'myimg.png',
'encoded_image': buffer
}, 'iso8859-1', 'myimg.png', 'image/png');
This is my encodeFormData
function, it works if the image is < 200x200 in size, however if I have a bigger image, the data is corrupt, I know this because when I post the data to the loadOneTab, the website says its corrupt -
Here is a screencast showing how i know it is corrupt - https://www.youtube.com/watch?v=OG_dkEVViPE
Here is my code -
function encodeFormData(data, charset, forArrBuf_nameDotExt, forArrBuf_mimeType) {
// http://stackoverflow.com/a/25020668/1828637
var encoder = Cc['@mozilla.org/intl/saveascharset;1'].createInstance(Ci.nsISaveAsCharset);
encoder.Init(charset || 'utf-8', Ci.nsISaveAsCharset.attr_EntityAfterCharsetConv + Ci.nsISaveAsCharset.attr_FallbackDecimalNCR, 0);
var encode = function(val, header) {
val = encoder.Convert(val);
if (header) {
val = val.replace(/\r\n/g, ' ').replace(/'/g, '\\\'');
}
return val;
}
var boundary = '----boundary--' + Date.now();
var mpis = Cc['@mozilla.org/io/multiplex-input-stream;1'].createInstance(Ci.nsIMultiplexInputStream);
var item = '';
for (var k of Object.keys(data)) {
item += '--' + boundary + '\r\n';
var v = data[k];
if (v.constructor.name == 'ArrayBuffer') {
item += 'Content-Disposition: form-data; name=\'' + encode(k, true) + '\';' + ' filename=\'' + forArrBuf_nameDotExt + '\'\r\n';
item += 'Content-Type: ' + forArrBuf_mimeType + '\r\n\r\n';
var ss = Cc['@mozilla.org/io/string-input-stream;1'].createInstance(Ci.nsIStringInputStream);
ss.data = item;
var abstream = Cc['@mozilla.org/io/arraybuffer-input-stream;1'].createInstance(Ci.nsIArrayBufferInputStream);
abstream.setData(v, 0, v.byteLength);
mpis.appendStream(ss);
mpis.appendStream(abstream);
item = '';
} else {
item += 'Content-Disposition: form-data; name=\'' + encode(k, true) + '\'\r\n\r\n';
item += encode(v);
}
item += '\r\n';
}
item += '--' + boundary + '--\r\n';
var ss = Cc['@mozilla.org/io/string-input-stream;1'].createInstance(Ci.nsIStringInputStream);
ss.data = item;
mpis.appendStream(ss);
var postStream = Cc['@mozilla.org/network/mime-input-stream;1'].createInstance(Ci.nsIMIMEInputStream);
postStream.addHeader('Content-Type', 'multipart/form-data; boundary=' + boundary);
postStream.setData(mpis);
postStream.addContentLength = true;
return postStream;
}