将base64图像数据转换为angularjs中的图像文件 [英] Convert base64 image data to image file in angularjs
本文介绍了将base64图像数据转换为angularjs中的图像文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在将base64文件转换为angularjs中的图像时获取损坏的文件任何人都可以建议我如何将base64文件转换为angularjs中的图像
getting corrupted file while converting base64 file to image in angularjs can anyone suggest me how to convert base64 file to image in angularjs
我正在使用此方法转换base64 file to image
I am using this method to convert base64 file to image
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');
推荐答案
首先,将dataURL转换为Blob
这样做
First, you convert dataURL to Blob do this
var blob = dataURItoBlob(imageBase64);
function dataURItoBlob(dataURI) {
// convert base64/URLEncoded data component to raw binary data held in a string
var byteString;
if (dataURI.split(',')[0].indexOf('base64') >= 0)
byteString = atob(dataURI.split(',')[1]);
else
byteString = unescape(dataURI.split(',')[1]);
// separate out the mime component
var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
// write the bytes of the string to a typed array
var ia = new Uint8Array(byteString.length);
for (var i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}
return new Blob([ia], {type:mimeString});
}
然后
var file = new File([blob], "fileName.jpeg", {
type: "'image/jpeg'"
});
这篇关于将base64图像数据转换为angularjs中的图像文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文