简体   繁体   English

如何从节点快递服务器下载文件?

[英]How to download files from node express server?

I have a file on my server (Node.js + Express).我的服务器上有一个文件(Node.js + Express)。 I need to download file from server to computer of user.我需要将文件从服务器下载到用户的计算机。 in React app I call the function which would download file:在 React 应用程序中,我调用了下载文件的函数:

downloadFileFromServer(file) { // file -- name of file
fetch(`${Config.baseUrl}/download-file/${this.props.params.idEvent}/${file}`, {
  method: 'GET'
})
    .then((response) => {
      if (response.status >= 400) {
        throw new Error('Bad response from server');
      }
      return response;
    });

} }

On backend I have this route:在后端,我有这条路线:

app.route('/download-file/:idEvent/:fileName')
.get((req, res) => {
  const id = `id${req.params.idEvent}`;
  const dir = `${Config.basePath}/${id}/${req.params.fileName}`;

  res.download(dir); // dir -- path to server's files. (something like this: 'var/www/... path to directory ...')
});

In result I haven't any results.结果我没有任何结果。 Nothing happend, consoles (frontend, backend) are empty, without errors.什么也没发生,控制台(前端,后端)是空的,没有错误。

Why I can't download file?为什么我不能下载文件? How to fix it?如何解决?

you have to install "js-file-download" library in react using following command您必须使用以下命令在反应中安装“js-file-download”库

npm install --save js-file-download

then the code in react file as follows:然后react文件中的代码如下:

 import download from 'js-file-download';
downloadFile = () => {
   axios.get("localhost:3030/getfile")
     .then(resp => {
            download(resp.data, fileName);
     });
}

the code at server side is as follows:服务器端的代码如下:

router.get("/getfile", (req, res) => {
  FileLocation ="public/FILE.pdf"
  file="File.pdf"
 res.download(fileLocation, file, (err) => {
                if (err) console.log(err);
            });
});

My reference for this answer is in this link .我对此答案的参考在此链接中

This works for me.I hope it works for you.这对我有用。我希望它对你有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 Node 服务器下载 React 中的文件? - How to download files in React from Node server? 如何从节点服务器下载文件(仅使用节点模块,不使用 express 等) - How to download a file from node server(using only node modules, no express, etc) 通过我的前端从节点快速服务器下载 JSON 文件 - Download JSON file from a node express server through my frontend 节点FTP从一台服务器下载文件并上传到另一台服务器 - Node FTP download files from one server and upload to another server 如何在A node.js / express服务器上接收一些文件并将文件发送到B node.js / express服务器 - How to receive some files at A node.js/express server and send files to B node.js/express server 使用 Express 从 NodeJS 服务器下载文件 - Download file from NodeJS Server using Express 使用 Express 从 NodeJS 服务器下载文件 - Download a file from NodeJS Server using Express 如何使用浏览器下载管理器从 express + mongoDB 服务器下载/流式传输文件 - How to download/stream a file from a express + mongoDB server, using the browser download manager 如何在Node / Express中将JS从客户端移动到服务器? - How to Move JS from Client to Server in Node/Express? 如何从远程服务器下载一堆文件,并使用Node.js按一定顺序将它们串联起来? - How do I download a bunch of files from a remote server, and concatenate them in a certain order using Node.js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM