问题
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?
回答1:
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.
回答2:
I found this post (Convert Data URI to File then append to FormData) to be quite helpful. If your file is already represented as a base64 encoded string, you would first need to create a blob representation from that and then you can use FormData append.
来源:https://stackoverflow.com/questions/26667820/upload-a-base64-encoded-image-using-formdata