Convert base64 data url to image file in angularjs

2020-06-03 07:07发布

问题:

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?

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

回答1:

you can generate blob from base64 data.

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

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.