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?
-
This question is already asked ultiple times over here why you isnt search for it ?Babar Hussain– Babar Hussain2017-03-29 18:50:50 +00:00Commented Mar 29, 2017 at 18:50
-
Did you mean put this base64 string into a JSON object to post data?F.Igor– F.Igor2017-03-29 18:53:33 +00:00Commented Mar 29, 2017 at 18:53
-
yes, base64 string into JSON object to post data.Sharath Bhat– Sharath Bhat2017-03-29 18:58:19 +00:00Commented Mar 29, 2017 at 18:58
Add a comment
|
2 Answers
Simple, set value to an keyObj.
var base64String = 'data:image/png;base64....'
var imgObj = {
url: base64String
}
<img src=''+ imgObj.url +''/>
3 Comments
Sharath Bhat
can access to the properties of the image file(height , width,etc..) with this code? If not how to do that.
Rui Costa
@SharathBhat yes, with
document.getElementById('#image-id').clientHeight or document.getElementById('#image-id').clientWidth for example.Neo
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 muchI 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