我有这个文件下载API
itemsRouter.route('/download/:id/:category/:type/:price/:name')
.get(function(req,res){
var currentDownloadIndex=0;
if(req.session.username){
// var uri = url.parse(req.url).id;
var filename = path.join(process.cwd(),'/uploads/files/'+ req.params.category+'/'+req.params.id+'.'+req.params.type);
path.exists(filename, function(exists) {
if(!exists) {
//file doesn't exist
res.redirect('/');
} else {
//if (fs.statSync(filename).isDirectory())
// filename += '/Justin Bieber - What Do You Mean.mp4';
try {
var stat = fs.statSync(filename);
res.writeHeader(200,{"Content-Length":stat.size});
var fReadStream = fs.createReadStream(filename);
fReadStream.pipe(res);
if(!(req.session.items[req.params.name]==req.params.name)){
req.session.price=parseFloat(req.session.price)+parseFloat(req.params.price);
req.session.items[req.params.name]=req.params.name;
currentDownloadIndex=currentDownloadIndex+1
}
// Todo : Generate a report after each success full download
}
catch (error) {
if(currentDownloadIndex==1){
req.session.price=parseFloat(req.session.price)-parseFloat(req.params.price);
req.session.items[req.session.items.length]=(req.params.name);
}
}
}
});
}else {
res.redirect('/');
}
});
这适用于视频文件只需在浏览器上下载文件但是当下载pdf文件时它会在浏览器中打开并且对此不满意,任何人都可以告诉我如何在没有任何混乱的情况下启动下载> 感谢
答案 0 :(得分:1)
将内容类型标题添加为" application / pdf"。