javascript 如果在 Chrome 上运行 createObjectURL 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10190647/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 08:58:58  来源:igfitidea点击:

createObjectURL doesnt work if run on Chrome

javascripthtmlcanvaswebsocket

提问by Eldon Lesley

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):

我试图从 websocket 服务器(在 .NET 中)检索图像我将图像作为字节发送然后我在客户端检索它,在客户端检索的代码(使用画布和 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

它完美地在 Firefox 上显示图片,但在 Chrome 上,它只是返回 undefined 并且不会通过 createObjectURL 加载图像我使用的是 Chrome 18.0.1025.162

any idea?

任何的想法?

回答by apsillers

From MDN:

来自MDN

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

此方法在 Chrome 和 Webkit 中的前缀为window.webkitURL.createObjectURL().

You should test if URLexists and then use the appropriate object:

您应该测试是否URL存在,然后使用适当的对象:

(window.URL ? URL : webkitURL).createObjectURL(evt.data);