0

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">

1 Answer 1

1

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: =

Sign up to request clarification or add additional context in comments.

3 Comments

this input take image
as i said above you can not store an image in a json object, you have to encode the image into base 64 then store as a json object and the decode the image somewhere else for display, please read the response before turning it down

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.