0

I have a function that gets images from an array

onFileChange(e) {
            let files = e.target.files;
            for (let file in files) {
                this.files.push(URL.createObjectURL(files[file]));
                if (files.hasOwnProperty(file)) {
                    console.log(files[file]);
                }
}

Which is triggered by this element:

<input multiple type="file" @change="onFileChange($event)"/>

My data object for this component is:

data() {
        return {
            files: [],
            url: null,
            uploadedFiles: []
}

When I render I get this error:

app.js:1920 Uncaught TypeError: Failed to execute 'createObjectURL' on 'URL': No function was found that matched the signature provided.

I am actually getting the file array. but with this error, help appreciated.

3 Answers 3

1

I still have no idea why this error occured yet i've found a workaround by switch from for/in to a regular "boring" for.

                for (var i = 0; i < files.length; i++) {
                var file = files[i],
                    src = (window.URL || window.webkitURL).createObjectURL(file);
                this.files.push(src);
                }

If there'll be an answer that would explain the error, ill vote it as the answer, this this is the best solve found by now.

Sign up to request clarification or add additional context in comments.

Comments

1

I think the problem is from this line this.files.push(URL.createObjectURL(files[file]));

It should be this.files.push(window.URL.createObjectURL(files[file])); in case of {}.toString.apply(files[file]) === '[object Blob]'

Otherwise,

const binaryData = [];
binaryData.push(files[file]);
window.URL.createObjectURL(new Blob(binaryData, {type: "application/zip"}))

Ref: Failed to execute 'createObjectURL'

Comments

0

I think your problem is not with for and for...in, it's with window.URL.
After you used this:

(window.URL || window.webkitURL).createObjectURL

Instead of this (URL => window.URL):

URL.createObjectURL

It worked for you, it's a browser compatibility issue.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.