I’ve been pulling my hair out for hours now with a question that has been asked a few times around here:
I have binary data from a flash audio recorder, which I turn into a dataURL and then want to convert to a blob for upload.
SO gave me several answers, which are all essentially the same:
- Blob from DataURL?
- How to convert dataURL to file object in javascript?
- Convert Data URI to File then append to FormData
I wrote the following code based on the above
var data = window.atob(dataURL.split(',')[1]);
var length = data.length;
var uInt8Array = new Uint8Array(length);
for (var i = 0; i < length; ++i) {
uInt8Array[i] = data.charCodeAt(i);
}
var myfile = new Blob([uInt8Array], {'type': 'audio/x-wav'});
This works perfectly in firefox nightly (20) and chrome 25, but chromium 20 keeps telling me the size of myfile is 19 bytes (when it should be several kb), and subsequent upload fails consistently.
Is this a bug in chromium, or did I miss a doc saying it is not supported yet? Any alternative ideas?
It looks like there’s a bug in chromium’s
Blob()constructor when called with certain parameters.I narrowed down the issue on jsfiddle (run in chromium to see the issue, other browsers seem to handle it properly, both blob sizes should be 32)
Here’s the code I put together to get around it:
I ended up running the first alternative, then
if (myfile.size == 19) {to get the second one if needed