I have this HTML page where you have a default image and if you click the "browse" button the image changes to the one you chose to open. Right now the chosen image shows up in the HTML, but it still has to be saved on the "/images" project folder.
Here's my code so far. I've been researching for a couple of days and everything I've tried so far hasn't worked. Also, I'm new to HTML and js, so excuse me if there are any beginner mistakes. Any ideas of what I need to add to the js code?
html:
<div class="cc-profile-image"><a href="#"><img src="images/photo.png" alt="Image"/></a></div><!--ORIGINAL PHOTO-->
<form action="images/photo.png" onchange="previewFile()" >
Select image to upload:
<input type="file" name="file" id="file" value>
<label for="file"> Select a file to upload</label>
<input type="submit" value="Subir">
</form>
js:
function previewFile(){
var preview = document.querySelector('img'); //selects the query named img
//preview.height = "512";
//preview.width = "512";
var file = document.querySelector('input[type=file]').files[0]; //sames as here
//file.height = "512";
//file.width = "512";
var reader = new FileReader();
reader.onloadend = function () {
preview.src = reader.result;
}
if (file) {
reader.readAsDataURL(file); //reads the data as a URL
} else {
preview.src = "";
}
}
PS: I'm not using any database, I simply want to store the uploaded photo to /images.