简体   繁体   中英

How to download a file from project/specific location using nodejs?

My requirement is I should be able to download an excel template on a click event, I thought I will put that excel template in some folder(docs) in my backend code(instead of generating it dynamically) and download that. Is that possible, if yes how? I am using express and node.

It is possible. Probably you use Express framework. Express can serve static files under '/public' folder. When user connect to this file, her/his browser will download file. But some browser can view files online. For example, Chrome can open PDF files. If you want to force to download file you can use this simple code;

app.get('/download', function(req, res){
var file = __dirname + '/upload-folder/dramaticpenguin.MOV';
  res.download(file); // Set disposition and send it.
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM