正文
方法一、
const subjectImg = new Promise((resolve, reject) => {
let xhr;
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
} else if (window.ActiveXObject) {
try {
xhr = new ActiveXObject('Msxml2.XMLHTTP');
} catch (e) {
try {
xhr = new ActiveXObject('Microsoft.XMLHTTP');
} catch (e) {}
}
}
if (xhr) {
xhr.onreadystatechange = function() {
if (xhr.status === 200 && xhr.readyState === 4) {
//获取响应文件流
const blob = xhr.response;
resolve(blob);
}
};
xhr.onerror = function(err) {
reject(err);
};
xhr.open('get', base64; //设置响应类型为blob类型 此处为base64文件
xhr.responseType = 'blob';
xhr.send(null);
} else {
reject('XMLHttpRequest创建失败');
}
});
subjectImg.then(function(value) {
data.subjectImg = value;
$form.set('subjectImg', value, `blob_${Date.now()}.jpg`); //设置为对应的base64文件类型 其中bmp在ie中有兼容性问题
});
方法二、
使用base64ToBlob包
var base64 = 'iVBORw0...ASUVORK5CYII=';
var blob = base64ToBlob(base64, 'image/png');
var url = window.URL.createObjectURL(blob);
总结
以上方法在Chrome浏览器上均可以使用,但是ie... 就让后台去转吧!