简体   繁体   中英

Download file from NodeJS Server using Express

I want to download a file from the server using node js. However, the download is not executed and the pdf content is entered as data as in the first image. I want to download like the second picture.

 var file_path = path.join(__dirname, '../PDF', category, itemName);

 console.log(file_path);//ex)C:\Users\zhfld\Desktop\cloudproject-master (4)\cloudproject-master\PDF\cosmetic\perfume
 res.setHeader('Content-disposition', 'attachment; filename=' + itemName + '.pdf');
 res.download(file_path + ".pdf");//ex)C:\Users\zhfld\Desktop\cloudproject-master (4)\cloudproject-master\PDF\cosmetic\perfume.pdf

网络消息(F12)

我想

If you are using express you can do that:

app.get('/download', function(req,res){
    const file = `${__dirname}/folder/file.pdf`;
    res.download(file);
}

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