废话不多说,直接上代码。
downloadFile(url) {
let xhr = new XMLHttpRequst();
xhr.open('GET', url); //url是后端给的下载文件地址
xhr.responseType = 'blob';
xhr.onload = () => {
let content = xhr.response;
let blob = new Blob([content]);
let file= new Blob([blob ], {type: 'docx'}); //这里type是文件类型
let formData = new FormData();
formData.append('files', file, 'testName.docx');
}
...
}