Search code examples
javascriptform-data

Upload a base64 encoded image using FormData?


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?


Solution

  • 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.