Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set content-type on blob

Tags:

javascript

We're transferring a Blob (image) down a websocket and rendering it to a canvas on the other end.

When I use createObjectURL with the blob, I get this warning:

Resource interpreted as Image but transferred with MIME type text/plain: "blob:https%3A//example.com/demo". 

We create the object URL using the following code. The blob is send via a standard websocket with socket.binaryType = "blob"; on the client side:

socket.onmessage = function(e) {   var blob = e.data;   var url = (window.URL || window.webkitURL).createObjectURL(blob);    var image = document.createElement('img');   image.src = url; } 

The only way I can think to address this warning is to create a copy of the blob with the following code, but I don't want to introduce the overhead of copying all the data:

var blob = new Blob([e.data], {   type: 'image/gif' }); 

The method gets called dozens of times per second.

Any ideas on how to set the blob content-type without creating a duplicate Blob object with new Blob?

like image 806
Chris Nolet Avatar asked Sep 25 '13 07:09

Chris Nolet


People also ask

How do I change the content type on azure blob?

Write-Verbose "Getting the container object named $ContainerName." Write-Verbose "Getting the blob object named $BlobName." Write-Verbose "Changing content type of '$BlobName' to '$ContentType'." Write-Host "Successfully changed content type of '$BlobName' to '$ContentType'."

What is BLOB content type?

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.

What kind of content is ideal for storing in BLOB storage?

Blob storage is optimized for storing massive amounts of unstructured data, such as text or binary data. Blob storage is ideal for: Serving images or documents directly to a browser. Storing files for distributed access.

What is blob metadata?

User-defined metadata: User-defined metadata consists of one or more name-value pairs that you specify for a Blob storage resource. You can use metadata to store additional values with the resource. Metadata values are for your own purposes only, and don't affect how the resource behaves.


1 Answers

Let's consider you have Blob instance (blob). You can use then slice method on it:

blob = blob.slice(0, blob.size, "image/jpeg") 

and that's it.

It just creates a new blob with the same data, but with the new type.

like image 169
turkus Avatar answered Sep 22 '22 04:09

turkus