Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

createObjectURL doesnt work if run on Chrome

Hello everyone,

I've tried to retrieve an image from a websocket server (in .NET) I send the image as bytes then I retrieve it on the client side, the code for retrieving on the client side (using canvas and JavaScript):

var c=document.GetElementById("myCanvas");
var ctx=c.getContext("2d");
ws.onmessage=function(evt)
{
    var image=new Image();
    image.src=URL.createObjectURL(evt.data);
    ctx.drawImage(image,0,0);
}

it perfectly displays the picture on firefox, but at Chrome, it just returning undefined and won't load the image through createObjectURL I'm using Chrome 18.0.1025.162

any idea?

like image 962
Eldon Lesley Avatar asked Apr 17 '12 11:04

Eldon Lesley


People also ask

Is createObjectURL deprecated?

Deprecated. Not for use in new websites.

What is revokeObjectURL?

revokeObjectURL() static method releases an existing object URL which was previously created by calling URL. createObjectURL() . Call this method when you've finished using an object URL to let the browser know not to keep the reference to the file any longer. Note: This feature is available in Web Workers.


1 Answers

From MDN:

This method is prefixed in Chrome and Webkit as window.webkitURL.createObjectURL().

You should test if URL exists and then use the appropriate object:

(window.URL ? URL : webkitURL).createObjectURL(evt.data);
like image 164
apsillers Avatar answered Nov 08 '22 23:11

apsillers