res.download function is not working in express.js


res.download function is not working in express.js



I want to download a file in node.js using express.js framework according to the get request url.



I didn't get an error in this code, but the file is not downloaded, it shows
success message.



Kindly look at following code, can anyone solve this issue?




var express=require("express");
var bodyparser=require("body-parser");
var app=express();
var path=require("path");
app.get("/download/:folder/:file(*)",function(req,res)
{

var folder=req.params.folder;
var filename=req.params.file;
var downloadlocation=path.join(__dirname,folder,filename);
console.log(downloadlocation);
res.download(downloadlocation,function(err,success)
{
if(err)
{
console.log("download failed try again");
}
else {
console.log("download successfully");
}
});

});

app.listen(8086,function()
{
console.log("server is listening on port 8086");
});





res.download(downloadlocation, filename, function(err,success)
– Lawrence Cherone
Jul 1 at 17:24


res.download(downloadlocation, filename, function(err,success)





Are you sure that it is not downloaded. The given code works fine here.
– t.niese
Jul 1 at 17:24





@LawrenceCherone the signature is res.download(path [, filename] [, options] [, fn]) so filename is optional. And because of the downloadlocation=path.join(__dirname,folder,filename) it would not make any difference here.
– t.niese
Jul 1 at 17:25



res.download(path [, filename] [, options] [, fn])


filename


downloadlocation=path.join(__dirname,folder,filename)





It is not working here
– demo developers
Jul 1 at 17:41





Can you try hard code a file url path and test whether it works?
– Marcia Ong
Jul 1 at 17:56









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Rothschild family

Cinema of Italy