I’m trying to use <input type="file">
to allow a user to upload an image that will be used as a background. I don’t know if I need to get the full path of the file or not.
This is the input I’m trying to use: <input type="file" name="selectBackground" id="selectBackground" accept="image/png, image/jpeg">
This is the JavaScript associated with the input
background = selectBackground.value; document.body.style.background = "#4d3325 url('" + background + "')no-repeat center center fixed"; document.body.style.backgroundSize = "auto 100%";
The background doesn’t change at all and when I try to display it as a regular image, it just shows a small image icon.
2
Answers
Hi friend check if this solution attends your needs:
Use URL.createObjectURL()
By using this, the uploaded image file is converted in to object url.
Finally when we change the other image we should remove the old url from memory for better memory management by using
URL.revokeObjectURL()
.