使用 Node js 和 Express 提供 pdf 文件

2024-04-26

所有的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);
   }); 
}

您可以使用express.static,尽早在您的应用程序中进行设置:

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

当浏览器导航到例如'/pdf/fooBar.pdf'。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Node js 和 Express 提供 pdf 文件 的相关文章