Upload a base64 encoded image using FormData?

I have jpeg as a base64 encoded string.

var image = "/9j/4AAQSkZJRgABAQEAS..." 

I want to upload this jpeg to the server using FormData.

 var data = new FormData(); 

What is the correct way to add an image to data?

+7
javascript form-data
source share
2 answers

Image data is nothing more than a string, so add it to your FormData object as follows:

 data.append("image_data", image); 

Then on your server side you can save this directly to the database or convert it to an image and save it to the file system. You can find this post .

+6
source share

I found this post ( Convert Data URI to File and then Add to FormData ) to be quite useful. If your file is already presented as a base64 encoded string, you first need to create a blob view, and then you can use the FormData application.

+4
source share

All Articles