a标签下载
function downloadFile(url){
let a= document.createElement(‘a‘);
a.href = file.url;
a.download = file.name; //文件名
a.target="_blank";
a.click();
}
a标签
download
属性只在同域下有效
bolb对象实现
downloadFile(fileUrl,fileName){
const xhr = new XMLHttpRequest();
xhr.open(‘GET‘,fileUrl,true);
xhr.responseType="bolb";
xhr.onload = function (){
if(xhr.status===200){
var a = document.createElement(‘a‘);
a.href = window.URL.createObjectURL(xhr.response);
a.download = fileName;//文件名
a.click();
}
}
xhr.send();
}