同域下
var
link = document.createElement(
'a'
);
link.href = file.url;
link.download = file.name;
link.target=
"_blank"
;
link.click();
跨域下
标签:重命名,url,前端,xhr,getBlob,link,blob,file,下载 From: https://blog.51cto.com/u_14976802/5932935function load(file) { this.getBlob(file.url).then(blob => { this.saveAs(blob, file.name); }); }
getBlob(url) {
return new Promise(resolve => {
const xhr = new XMLHttpRequest();xhr.open('GET', url, true);
xhr.responseType = 'blob';
xhr.onload = () => {
if (xhr.status === 200) {
resolve(xhr.response);
}
};xhr.send();
});
}
//下载文件
saveAs(blob, filename) {
var
link = document.createElement(
'a'
);
link.href = window.URL.createObjectURL(blob);
link.download = filename;
link.click();
}