【发布时间】:2019-03-04 12:09:01
【问题描述】:
为了练习我的节点,我正在尝试制作一个文件下载器。目的是从数据库(当前为 mp3)中获取文件的 url,将其下载到项目中并将其用作客户端的 href。
我已经成功实现了承诺并下载了一个文件,但是文件说它是空的,但是当我进入项目时,我可以很好地打开文件(mp3)。
这是我目前所拥有的:
.then(() => {
Promise.resolve()
.then(() => {
return new Promise(resolve => {
console.log("Started download");
// req.body.total_source is the full url of the file
console.log(req.body.total_source);
setTimeout(() => {
download(req.body.total_source, "dist").then(() => {
console.log("downloaded!");
resolve();
});
}, 1000);
});
})
.then(() => {
console.log("success page rendered");
res.render("success", {
// only get file name from url
song_link: req.body.total_source.substring(
req.body.total_source.lastIndexOf("/") + 1,
req.body.total_source.length
),
song_source: req.body.total_source,
name: req.body.name
});
});
})
并且按钮设置为(使用ejs):
<a href="/dist/<%= song_link %>" download>Download <%= name %></a>
谁能指出我正确的方向? console.logs 都显示在成功页面呈现之前下载已完成,所以我不确定为什么它会显示“失败 - 空文件”。
【问题讨论】:
标签: javascript node.js express download ejs