/*** 下载文件 */
onDownLoad(path, name) {
const xhr = new XMLHttpRequest();
xhr.open('GET', path, true);
xhr.responseType = 'blob';
xhr.onload = function() {
if (this.status === 200) {
const fileName = name;
// 响应数据处理
const blob = new Blob([this.response]);
const blobUrl = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = blobUrl;
a.download = fileName;
a.click();
window.URL.revokeObjectURL(blobUrl);
}
};
xhr.send();
}
}
前端通过请求后端接口下载文件
最新推荐文章于 2024-06-07 15:29:04 发布