将base64数据URL转换为angularjs中的图像文件

问题描述:

我的angularJs控制器中有base64数据URL,我需要一个图像文件,以便可以通过ajax将其作为多部分数据发送到服务器吗?

I have base64 data URL in my angularJs controller, and I need an image file from that, so that I could send it to server as multi-part data through ajax?

我正在寻找诸如angularjs中的文件编写器之类的东西. 有人可以帮我吗?

I'm looking for something like file writer in angularjs. Can anyone help me please?

您可以从base64数据生成blob.

you can generate blob from base64 data.

var imageBase64 = "image base64 data";
var blob = new Blob([imageBase64], {type: 'image/png'});

从该blob中,您可以生成文件对象.

From this blob, you can generate file object.

var file = new File([blob], 'imageFileName.png');

您可以使用此文件对象,并使用多部分数据将其发布到服务器.

You can use this file object and post it to server using multipart data.