I want to store uploded file as json object how can i achive that can anyone help
<input type="file" accept="image/*" name="image" id="image">
Not sure what this input is for but I have a code example that takes that input in a form and uses js to listen for submission and convert the submission data into json
Here:
<form>
+ <label for="name">Name</label>
+ <input type="text" name="name" id="name" />
+
<label for="email">Email</label>
<input type="email" name="email" id="email" />
<button type="submit">Submit</button>
</form>
and the js:
> function handleSubmit(event) {
> event.preventDefault();
>
> const data = new FormData(event.target);
>
> + const value = Object.fromEntries(data.entries());
>
> console.log({ value }); }
>
> const form = document.querySelector('form');
> form.addEventListener('submit', handleSubmit);
UPDATE:
For an image:
The JSON format can contain only those types of value:
string number object array true false null An image is of the type "binary" which is none of those. So you can't directly insert an image into JSON. What you can do is convert the image to a textual representation which can then be used as a normal string.
The most common way to achieve that is with what's called base64. Basically, instead of encoding it as 1 and 0s, it uses a range of 64 characters which makes the textual representation of it more compact. So for example the number '64' in binary is represented as 1000000, while in base64 it's simply one character: =