使用节点js&提供pdf文件表示 [英] serve pdf files using node js & express

查看:36
本文介绍了使用节点js&提供pdf文件表示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

All the PDF files are saved in the filesystem on the server, how to make the files to be downloadable in client side.

例如:

 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 ,并在您的应用中进行早期设置:

You can use express.static, set it up early in your app:

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

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

And it will automatically do the job for you when browser navigates to e.g. '/pdf/fooBar.pdf'.

这篇关于使用节点js&提供pdf文件表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆