【问题标题】:Node download method produces empty file节点下载方法产生空文件
【发布时间】: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


    【解决方案1】:

    已修复:我认为问题在于,当我将项目设置为使用“公共”文件夹中的客户端资产时,我将文件保存到 dist 文件夹。将文件保存到“/public/assets”已修复问题:

    download(req.body.total_source, "public/assets");
    

    【讨论】:

      猜你喜欢
      • 2016-03-07
      • 2019-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-18
      • 1970-01-01
      • 2021-01-06
      相关资源
      最近更新 更多