背景:下载图片和word,接口返回的图片是base64,word返回的是文件流
(后台java要用apache的base64转图片,不能用java自带的base64解析图片)
针对IE9已上代码参考
针对谷歌火狐
代码参考:
if(navigator.userAgent.indexOf("MSIE 9.0")>0){
layer.msg('抱歉,此功能仅支持IE10及以上');
return
}
let imgUrl = "data:image/png;base64," + res;
let blob = this.base64ToBlob(imgUrl, res)
var a = document.createElement('a')
document.body.appendChild(a);
a.style.display='none';
a.href = blob
a.download = this.selectedBarcode + '.png'
console.log('download ??')
console.log('window.navigator.msSaveOrOpenBlob ??')
if (window.navigator.msSaveOrOpenBlob) {
return window.navigator.msSaveOrOpenBlob(blob, this.selectedBarcode + '.png');
}
let evt = document.createEvent("HTMLEvents");
evt.initEvent("click", true, true);
a.href = URL.createObjectURL(blob);
a.click();
if(navigator.userAgent.indexOf("MSIE 9.0")>0){
layer.msg('抱歉,此功能仅支持IE10及以上');
} else {
let blob = new Blob([res], {type: 'application/vnd.ms-word;charset=UTF-8'});
let fileName = this.selectedBarcode + '.doc';
if (window.navigator.msSaveOrOpenBlob) {
navigator.msSaveBlob(blob, fileName)
} else {
var link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.download = fileName;
link.click();
window.URL.revokeObjectURL(link.href) //释放内存
}
}