I need to convert my image to a base64 string so that i can send my image to a server. Is there any js file for this... ? Else how to convert it
Join them; it only takes a minute:
|
You can use the HTML5 Create a canvas, load your image into it and then use |
|||||||||||||
|
There are two approaches you can choose from: 1. Approach: FileReaderLoad the image as blob via XMLHttpRequest and use the FileReader API to convert it to a data URL.
This code example could also be implemented using the fetch api (example). This approach:
Usage:
Browser Support: 2. Approach: CanvasLoad the image into an Image-Object, paint it to a non tainted canvas and convert the canvas back to a dataURL.
Usage:
Supported input formats:
Supported output formats:
Browser Support:
Demo:Images from the local file systemIf you want to convert images from the users file system you need to take a different approach. Use the FileReader API (Check out this fiddle). |
|||||||||||||||||||||
|
This snippet could convert your string,image and even video file to base64 string data. Try it once...
|
|||||||||
|
Here is what i did
And here is how you use it
|
|||
|
As far as i know image can be converted into base64 string either by FileReader() or storing it in canvas element and then use toDataURL() to get image.I had the simmilar kind of problem you can refer this. |
|||
|
You could use FileAPI, but it's pretty much unsupported. |
||||
|
Well, if you are using dojo, it gives us direct way to encode or decode into base64. Try this: To encode an array of bytes using dojox.encoding.base64:
To decode a base64-encoded string:
|
||||
|
protected by acdcjunior Mar 22 '15 at 23:19
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?