Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: Failed to execute 'createObjectURL' on 'URL': Overload resolution failed

I am trying to have a file upload feature in my React application but am running into a problem. When I try to upload a first picture, it works just fine. The file explorer dialog closes and my picture is displayed. Overwriting the picture with another picture from my file explorer also works.

However, when I cancel the file explorer while overwriting, I get the following error:

TypeError: Failed to execute 'createObjectURL' on 'URL': Overload resolution failed.

Here is my relevant code:

showImage = (e) =>{
    this.setState({image: URL.createObjectURL(e.target.files[0])})
  }

render() {
    return (
 <div className="content">
        <input
          accept="image/*"
          className="input"
          id="icon-button-file"
          type="file"
          onChange={this.showImage}
        />
        <label htmlFor="icon-button-file">
       
          <IconButton
            className="image"
            aria-label="upload picture"
            component="span"
          >
             { this.state.image == null ? <AddAPhotoIcon className="icon" /> : 
             <img src={this.state.image} alt="test" className="picture"/> }
             </IconButton>
        </label>
</div>
)
like image 340
Pimv_h Avatar asked Apr 06 '21 14:04

Pimv_h


People also ask

What is URL createObjectURL?

The URL. createObjectURL() static method creates a string containing a URL representing the object given in the parameter. The URL lifetime is tied to the document in the window on which it was created. The new object URL represents the specified File object or Blob object.

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.

Why createobjecturl failed to execute on'url'?

Failed to execute 'createObjectURL' on 'URL': No function was found that matched the signature provided. Show activity on this post. Show activity on this post. This error is caused because the function createObjectURL no longer accepts media stream object for Google Chrome This worked for me. Show activity on this post.

What does overload resolution failed mean?

Overload resolution failed means that the URL.createObjectURL (e.data.urlOrBlob) received undefined value. You can try this on Chrome console and you will see the error,

Why is my createobjecturl not working in chrome?

This error is caused because the function createObjectURL no longer accepts media stream object for Google Chrome This worked for me. Show activity on this post. My code was broken because I was using a deprecated technique.

What happened to the URL createobjecturl () method in mediastream?

The URL.createObjectURL () method has been removed from the MediaStream interface. This method has been deprecated in 2013 and superseded by assigning streams to HTMLMediaElement.srcObject.


1 Answers

I think the error means that the files array could be empty. You perhaps want to check if the array is empty before accessing a member.

if(e.target.files.length !== 0){
      this.setState({image: URL.createObjectURL(e.target.files[0])})
    }
like image 90
yourivdloo Avatar answered Oct 06 '22 00:10

yourivdloo