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>
)
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.
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.
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.
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,
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.
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.
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])})
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With