使用节点js&提供pdf文件表达

时间:2013-07-24 10:55:26

标签: node.js pdf express

所有PDF文件都保存在服务器上的文件系统中,如何在客户端下载文件。

代表:

 app.use('/pdfDownload', function(req, res){
  var pathToTheFile = req.body.fileName;
   readFile(pathToTheFile, function(data){
      //code to make the data to be downloadable;
    });
 });

是提出的请求

function readFile(pathToTheFile, cb){
   var fs = require('fs');
   fs.readFile(pathToTheFile, function(err, data){
      //how to make the file fetched to be downloadable in the client requested
      //cb(data);
   }); 
}

1 个答案:

答案 0 :(得分:6)

您可以使用express.static,在应用中尽早设置:

app.use('/pdf', express.static(__dirname + '/pathToPDF'));

当浏览器导航到例如,它将自动为您完成工作。 '/pdf/fooBar.pdf'。

相关问题