Upload a base64 encoded image using FormData?

user1031947 picture user1031947 · Oct 31, 2014 · Viewed 47.8k times · Source

I have a jpeg as a base64 encoded string.

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

I would like to upload this jpeg to the server using FormData.

var data = new FormData();

What is the proper way to append the image to data?

Answer

HeadCode picture HeadCode · Oct 31, 2014

Your image data is nothing more than a string, so append it to your FormData object like this:

data.append("image_data", image);

Then on your server side you can store that directly in a database or convert it to an image and store it on the file system. You might find this post helpful.