0

I wanted to convert a image file into JSON object in javascript. So I have converted image into a string using base64 encoding. How to convert this string to JSON object?

3
  • This question is already asked ultiple times over here why you isnt search for it ? Commented Mar 29, 2017 at 18:50
  • Did you mean put this base64 string into a JSON object to post data? Commented Mar 29, 2017 at 18:53
  • yes, base64 string into JSON object to post data. Commented Mar 29, 2017 at 18:58

2 Answers 2

2

Simple, set value to an keyObj.

    var base64String = 'data:image/png;base64....'
    var imgObj = {
      url: base64String
    }

    <img src=''+ imgObj.url +''/>
Sign up to request clarification or add additional context in comments.

3 Comments

can access to the properties of the image file(height , width,etc..) with this code? If not how to do that.
@SharathBhat yes, with document.getElementById('#image-id').clientHeight or document.getElementById('#image-id').clientWidth for example.
I have tried this code with .obj file, but it's unsucessful. The base64String of .obj file look like: data:;base64...... Could you please give me some suggestion? Thank you so much
0

I would create a new Image() and set the src to the image. Then onload of the image, draw it to a canvas (which can be in memory, doesn't need to be in the DOM), and from there use toDataURL(). See this page for more details: https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toDataURL

Comments

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.