Express服务器发送空PDF文件 [英] Express server send empty PDF file

查看:135
本文介绍了Express服务器发送空PDF文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  app.get('/ teste',function(req, res,next){
res.setHeader('content-type','application / pdf');
res.download(app.get('appPath')+'/ teste.pdf') ;
}

我尝试使用或多或少做同样的事情的其他解决方案: / p>

  app.get('/ teste',function(req,res,next){
res.setHeader content-type','application / pdf');
fs.createReadStream(app.get('appPath')+'/ teste.pdf')。pipe(res);
}

  app.get('/ teste',function(req,res,next){
res.setHeader('content-type','application / pdf');
res.sendfile(app。 get('appPath')+'/ teste.pdf');
}

我的问题是当我在浏览器中问这个路由时,我收到一个与原始文件相同数量的页面的空的pdf文件/ p>

我使用 app.use(express.bodyParser()); 。配置我的快递服务器。



任何人都可以帮助我?

解决方案

使用connect-livereload中间件。问题是connect-livereload将js代码片段注入到pdf数据流中。它也可能导致其他非html数据的问题。好消息是,这只会在开发过程中引起问题(您不应该在生产中加载此中间件。)



最近修复了很多模板,一个较旧的版本,所以检查你的package.json文件,并获得最新版本,如果必要的话。最新的connect-livereload版本是0.5.3。


I have a route that sends a pdf file:

app.get('/teste',function(req,res,next){
    res.setHeader('content-type','application/pdf');
    res.download(app.get('appPath')+'/teste.pdf');
}

I tried use another solutions that do more or less the same thing:

app.get('/teste',function(req,res,next){
    res.setHeader('content-type','application/pdf');
    fs.createReadStream(app.get('appPath')+'/teste.pdf').pipe(res);
 }

and

app.get('/teste',function(req,res,next){
        res.setHeader('content-type','application/pdf');
        res.sendfile(app.get('appPath')+'/teste.pdf');
}

My problem is when I ask this route in browser and I receive an empty pdf file with the same number of pages that the original file.

I configured my express server with app.use(express.bodyParser());.

Anyone can help me?

解决方案

I've seen this when using connect-livereload middleware. The problem is that connect-livereload is injecting a js code snippet into the pdf data stream. It can also cause problems with other non-html data. The good news is that this should only cause problems during development (you shouldn't be loading this middleware in production.)

This was recently fixed but a lot of templates include an older version so check your package.json file and get the latest version if necessary. The most recent connect-livereload version is 0.5.3.

这篇关于Express服务器发送空PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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