下载远程文件时需要对文件重命名,可以通过如下方法处理,远程链接需要支持跨域。
/**
* 处理点击下载按钮调用
**/
handleDownloadFlie(flie) {
this.getBlob(flie.url).then(blob => {
this.DownloadItem(blob, flie.name);
});
},
/**
* 通过传入的url拿到blob对象
**/
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();
});
},
/**
* 执行文件下载操作
**/
DownloadItem(blob, fileName) {
const link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.download = fileName;
link.click();
}