Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blob constructor browser compatibility

I am developping an application where I recieve image data stored in a uint8Array. I then transform this data to a Blob and then build the image url.

Simplified code to get data from server:

var array;  var req = new XMLHttpRequest(); var url = "img/" + uuid + "_" +segmentNumber+".jpg"; req.open("GET", url, true); req.responseType = "arraybuffer"; req.onload = function(oEvent) {     var data = req.response;         array = new Int8Array(data);       }; 

Constructor:

out = new Blob([data], {type : datatype} ); 

The Blob contsructor is causing problem. It works fine on all browsers except Chrome on mobile and desktop devices.

Use of Blob:

// Receive Uint8Array using AJAX here // array = ... // Create BLOB var jpeg = new Blob( [array.buffer], {type : "image/jpeg"}); var url = DOMURL.createObjectURL(jpeg); img.src = url; 

Desktop Chrome gives me a warnning : ArrayBuffer values are deprecated in Blob Constructor. Use ArrayBufferView instead.

Mobile Chrome gives me an error: illegal constructor

If I change the constructor to work on Chrome it fails on other browsers.

like image 497
Jacob Avatar asked Mar 08 '13 12:03

Jacob


People also ask

How do I open a blob file in browser?

If you cannot open your BLOB file correctly, try to right-click or long-press the file. Then click "Open with" and choose an application. You can also display a BLOB file directly in the browser: Just drag the file onto this browser window and drop it.

How do I open blob URL in Chrome?

To open Blob URL on Chrome iOS with JavaScript, we can use the FileReader constructor. For instance, we write: const reader = new FileReader(); const out = new Blob(['abc'], { type: 'text/plain' }); reader.

What is blob in browser?

The Blob object represents a blob, which is a file-like object of immutable, raw data; they can be read as text or binary data, or converted into a ReadableStream so its methods can be used for processing the data. Blobs can represent data that isn't necessarily in a JavaScript-native format.

Where are blobs stored browser?

A Blob is stored in the memory much like any other ArrayBuffer . It's stored in the ram, just like the other objects declared in the window. Looking at the chrome://blob-internals , we can see how its physically stored in the ram.


2 Answers

So, these are actually two different problems. The Desktop Chrome warning was a bug in chrome which is fixed since 2013-03-21.

Mobile Chrome is giving you a TypeError because the blob constructor is not supported. Instead you must use the old BlobBuilder API. The BlobBuilder API has browser specific BlobBuilder constructors. This is the case for FF6 - 12, Chrome 8-19, Mobile Chrome, IE10 and Android 3.0-4.2.

var array = new Int8Array([17, -45.3]);  try{   var jpeg = new Blob( [array], {type : "image/jpeg"}); } catch(e){     // TypeError old chrome and FF     window.BlobBuilder = window.BlobBuilder ||                           window.WebKitBlobBuilder ||                           window.MozBlobBuilder ||                           window.MSBlobBuilder;     if(e.name == 'TypeError' && window.BlobBuilder){         var bb = new BlobBuilder();         bb.append(array.buffer);         var jpeg = bb.getBlob("image/jpeg");     }     else if(e.name == "InvalidStateError"){         // InvalidStateError (tested on FF13 WinXP)         var jpeg = new Blob( [array.buffer], {type : "image/jpeg"});     }     else{         // We're screwed, blob constructor unsupported entirely        } } 

Fiddle: http://jsfiddle.net/Jz8U3/13/

like image 145
JoeyP Avatar answered Sep 25 '22 15:09

JoeyP


Got it working with your code. I only had to change some little detail:

if(e.name == 'TypeError' && window.BlobBuilder){         var bb = new BlobBuilder();         bb.append(data);         out = bb.getBlob(datatype);         console.debug("case 2");     } 

bb.append(data); // data must be with no brackets

My function(constructor) that works now for all browsers:

var NewBlob = function(data, datatype) {     var out;      try {         out = new Blob([data], {type: datatype});         console.debug("case 1");     }     catch (e) {         window.BlobBuilder = window.BlobBuilder ||                 window.WebKitBlobBuilder ||                 window.MozBlobBuilder ||                 window.MSBlobBuilder;          if (e.name == 'TypeError' && window.BlobBuilder) {             var bb = new BlobBuilder();             bb.append(data);             out = bb.getBlob(datatype);             console.debug("case 2");         }         else if (e.name == "InvalidStateError") {             // InvalidStateError (tested on FF13 WinXP)             out = new Blob([data], {type: datatype});             console.debug("case 3");         }         else {             // We're screwed, blob constructor unsupported entirely                console.debug("Errore");         }     }     return out; } 
like image 27
Jacob Avatar answered Sep 25 '22 15:09

Jacob