Javascript 无法在“URL”上执行“createObjectURL”:
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27120757/
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
Failed to execute 'createObjectURL' on 'URL':
提问by Hardik Mandankaa
Display Below error in Safari.
在 Safari 中显示以下错误。
Failed to execute 'createObjectURL' on 'URL': No function was found that matched the signature provided.
无法在“URL”上执行“createObjectURL”:找不到与提供的签名匹配的函数。
My Code is:
我的代码是:
function createObjectURL(object) {
return (window.URL) ? window.URL.createObjectURL(object) : window.webkitURL.createObjectURL(object);
}
This is my Code for image:
这是我的图像代码:
function myUploadOnChangeFunction() {
if (this.files.length) {
for (var i in this.files) {
if (this.files.hasOwnProperty(i)) {
var src = createObjectURL(this.files[i]);
var image = new Image();
image.src = src;
imagSRC = src;
$('#img').attr('src', src);
}
}
}
}
回答by mimo
UPDATE
更新
Consider avoiding createObjectURL()method, while browsers are disabling support for it. Just attach MediaStreamobject directly to the srcObjectproperty of HTMLMediaElemente.g. <video>element.
考虑避免createObjectURL()方法,而浏览器正在禁用对它的支持。只需将MediaStream对象直接附加到例如元素的srcObject属性。HTMLMediaElement<video>
const mediaStream = new MediaStream();
const video = document.getElementById('video-player');
video.srcObject = mediaStream;
However, if you need to work with MediaSource, Blobor File, you have to create a URL with URL.createObjectURL()and assign it to HTMLMediaElement.src.
但是,如果您需要使用MediaSource、Blob或File,则必须使用 来创建 URLURL.createObjectURL()并将其分配给HTMLMediaElement.src。
Read more details here: https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/srcObject
在此处阅读更多详细信息:https: //developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/srcObject
Older Answer
较旧的答案
I experienced same error, when I passed to createObjectURLraw data:
当我传递给createObjectURL原始数据时,我遇到了同样的错误:
window.URL.createObjectURL(data)
It has to be Blob, Fileor MediaSourceobject, not data itself. This worked for me:
它必须是Blob,File或MediaSource对象,而不是数据本身。这对我有用:
var binaryData = [];
binaryData.push(data);
window.URL.createObjectURL(new Blob(binaryData, {type: "application/zip"}))
Check also the MDN for more info: https://developer.mozilla.org/en-US/docs/Web/API/URL/createObjectURL
另请查看 MDN 以获取更多信息:https: //developer.mozilla.org/en-US/docs/Web/API/URL/createObjectURL
回答by Christian
This error is caused because the function createObjectURLis deprecated for Google Chrome
导致此错误的原因createObjectURL是 Google Chrome 不推荐使用该功能
I changed this:
我改变了这个:
video.src=vendorUrl.createObjectURL(stream);
video.play();
to this:
对此:
video.srcObject=stream;
video.play();
This worked for me.
这对我有用。
回答by Dani Amsalem
My code was broken because I was using a deprecated technique. It used to be this:
我的代码被破坏了,因为我使用了不推荐使用的技术。以前是这样的:
video.src = window.URL.createObjectURL(localMediaStream);
video.play();
Then I replaced that with this:
然后我用这个替换了它:
video.srcObject = localMediaStream;
video.play();
That worked beautifully.
那效果很好。
EDIT: Recently localMediaStreamhas been deprecated and replaced with MediaStream. The latest code looks like this:
编辑:最近localMediaStream已被弃用并替换为MediaStream. 最新的代码如下所示:
video.srcObject = MediaStream;
References:
参考:
- Deprecated technique: https://developer.mozilla.org/en-US/docs/Web/API/URL/createObjectURL
- Modern deprecated technique: https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/srcObject
- Modern technique: https://developer.mozilla.org/en-US/docs/Web/API/MediaStream
回答by max.kovpak
I had the same error for the MediaStream. The solution is set a stream to the srcObject.
我对 MediaStream 有同样的错误。解决方案是为 srcObject 设置一个流。
From the docs:
从文档:
Important: If you still have code that relies on createObjectURL() to attach streams to media elements, you need to update your code to simply set srcObject to the MediaStream directly.
重要提示:如果您仍然有依赖 createObjectURL() 将流附加到媒体元素的代码,您需要更新您的代码以直接将 srcObject 设置为 MediaStream。
回答by eddyparkinson
Video with fall back:
回退视频:
try {
video.srcObject = mediaSource;
} catch (error) {
video.src = URL.createObjectURL(mediaSource);
}
video.play();
From: https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/srcObject
来自:https: //developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/srcObject
回答by Hiago Takaki
The problem is that the keys provided in the loop do not refer to the index of the file.
问题是循环中提供的键不引用文件的索引。
for (var i in this.files) {
console.log(i);
}
The output of the above code is:
上面代码的输出是:
0
length
item
But what was expected was:
但预期的是:
0
1
2
etc...
Then the error occurs when the browser tries to execute, for example:
然后在浏览器尝试执行时出现错误,例如:
window.URL.createObjectURL(this.files["length"])
I suggest implementation based on the following code:
我建议基于以下代码实现:
var files = this.files;
for (var i = 0; i < files.length; i++) {
var file = files[i],
src = (window.URL || window.webkitURL).createObjectURL(file);
...
}
I hope this can help someone.
我希望这可以帮助某人。
Greetings!
你好!
回答by Guilherme Porto
If you are using ajax, it is possible to add the options xhrFields: { responseType: 'blob' }:
如果您使用的是 ajax,则可以添加选项xhrFields: { responseType: 'blob' }:
$.ajax({
url: 'yourURL',
type: 'POST',
data: yourData,
xhrFields: { responseType: 'blob' },
success: function (data, textStatus, jqXHR) {
let src = window.URL.createObjectURL(data);
}
});
回答by AutoCiudad
I fixed it downloading the latest version from GgitHub GitHub url
我修复了从 GgitHub GitHub url下载最新版本的问题

