使用 saveAs 下载文件
npm install file-saver --save // 安装 依赖
import { saveAs } from 'file-saver'; // 导入依赖
let text = "Hello, world!" // 数据流
let blob = new Blob([text], {type: "text/plain;charset=utf-8"}); // 设置下载信息
saveAs(blob,"数据.txt"); // 保存流数据 //注意:文件名是什么格式就下载就是什么格式的文件
responseType: 'blob', // 接口api 里声明数据类型
// 预览
// handlePreview(obj) {
// let url;
// if (url) {
// URL.revokeObjectURL(url); // 每次显示新的预览图片时,先释放上一次的图片内存
// }
// url = URL.createObjectURL(obj.raw);
// window.open(url);
// }
文件下载 使用 Blob 流 下载
const blob = new Blob([res]);
const url = URL.createObjectURL(blob);
let a = document.createElement('a');
a.href = url;
a.download = 'wenmu.zip';
a.click();
URL.revokeObjectURL(url);
使用浏览器 下载文件
window.open('http://127.0.0.1:8088/upload/stream');
a 标签下载
<a href="http://localhost:8080/a.png" download="a.png">下载</a>
文件上传
let fd = new FormData();
fd .append('file', file); // file 上传的文件
fd .append('otherParams', otherParams); // 其他参数
// 如果有多个 遍历追加到 fd 里面
//uploadFileList.forEach((f) => {
// fd.append("file", f.raw);
// });
tips: responseType: “blob”,