简体   繁体   中英

How to send base64 encoded image string into JSON object in javascript?

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?

Simple, set value to an keyObj.

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

    <img src=''+ imgObj.url +''/>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM