egg.js文件下载实现
2021-05-28 12:00
标签:链接 访问 合并 att ons doc 推荐 create multi 多文件合并下载 前端可以浏览器直接访问对应链接,也可以用fetch 单文件下载 下载字符串内容到文件 推荐阅读: https://www.zhihu.com/question/59351806 最后更新时间: 2021.5.17 egg.js文件下载实现 标签:链接 访问 合并 att ons doc 推荐 create multi 原文地址:https://www.cnblogs.com/elimsc/p/14779088.html
依赖于 https://github.com/feross/multistreamconst streams = [];
for (const file of files) {
streams.push(fs.createReadStream(file));
}
this.ctx.attachment(‘filename‘);
this.ctx.set(‘Content-Type‘, ‘application/octet-stream‘);
this.ctx.body = new MultiStream(streams); // new MultiStream(streams).pipe(res)
fetch(url).then(res => res.blob()).then(blob => {
let a = document.createElement(‘a‘);
let url = window.URL.createObjectURL(blob);
a.href = url;
a.download = `filename.txt`;
a.click();
window.URL.revokeObjectURL(url);
a = null;
});
this.ctx.set(‘Content-Type‘, ‘application/octet-stream‘);
this.ctx.body = fs.createReadStream(filePath)
this.ctx.set(‘Content-Type‘, ‘application/octet-stream‘);
this.ctx.body = str;