Upload a base64 encoded image using FormData?

天涯浪子 提交于 2019-11-29 18:55:58

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!