implementation of basic image local storage

This commit is contained in:
Henry Feng 2022-11-17 16:17:51 -08:00
parent 540d6563d8
commit 416d26658d
2 changed files with 49 additions and 4 deletions

View File

@ -43,6 +43,44 @@ function initFormHandler() {
//accessing form components
let tagContainer = document.getElementById("tag-container-form");
let form = document.querySelector("form");
/*
* change the input source of the image between local file and URL
* depending on user's selection
*/
let select = document.getElementById("select");
select.addEventListener("change", function() {
const input = document.getElementById('source');
if (select.value == "file") {
input.innerHTML = `
Source:
<input type="file" id="mealImg" name="mealImg">
`
}
else {
input.innerHTML = `
Source:
<input type="text" id="mealImg" name="mealImg">
`
}
});
//addressing sourcing image from local file
let imgDataURL = "";
document.getElementById("mealImg").addEventListener("change", function() {
const reader = new FileReader();
//store image data URL after successful image load
reader.addEventListener("load", ()=>{
imgDataURL = reader.result;
}, false);
//convert image file into data URL for local storage
reader.readAsDataURL(document.getElementById("mealImg").files[0]);
})
form.addEventListener("submit", function(){
/*
@ -51,12 +89,16 @@ function initFormHandler() {
*/
let formData = new FormData(form);
let reviewObject = {};
for (let [key, value] of formData) {
console.log(`${key}`);
console.log(`${value}`);
if (`${key}` !== "tag-form") {
reviewObject[`${key}`] = `${value}`;
}
if (`${key}` === "mealImg" && select.value == "file") {
reviewObject["mealImg"] = imgDataURL;
}
}
reviewObject["tags"] = [];

View File

@ -10,7 +10,6 @@
<!-- Recipe Card Custom Element -->
<script src="assets/scripts/ReviewCard.js" type="module"></script>
<!-- Main Stylesheets & Scripts -->
<!-- Temporarily commented out reset.css due to furthur discussion needed on the values of the default config-->
<!-- <link rel="stylesheet" href="/static/reset.css" /> -->
@ -26,9 +25,14 @@
<!----> <form id="new-food-entry">
<fieldset>
<legend>Pic:</legend>
<label for="mealImage">
Choose Input type:
<select id="select" name="select">
<option value="file">File Upload</option>
<option value="url">From an URL</option>
</select>
<label for="mealImage" id="source">
Source:
<input type="text" id="mealImg" name="mealImg">
<input type="file" id="mealImg" name="mealImg">
</label>
<label for="image-alt">
Alt Text:
@ -36,7 +40,6 @@
</label>
</fieldset>
<fieldset>
<legend> Meal: </legend>
<label for="Meal: ">Meal:
<input type="text" id="mealName" name="mealName" required>