Failed to execute 'createObjectURL' on 'URL':

匿名 (未验证) 提交于 2019-12-03 01:18:02

问题:

Display Below error in Safari.

Failed to execute 'createObjectURL' on 'URL': No function was found that matched the signature provided. 

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);                } }                        } }  

回答1:

I experienced same error, when I passed to createObjectURL raw data:

window.URL.createObjectURL(data) 

It has to be Blob or File object, not data itself. This worked for me:

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



回答2:

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 

I hope this can help someone.

Greetings!



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!