使用expressjs时,下载的.pdf文件已损坏 [英] Downloaded .pdf files are corrupted when using expressjs

查看:252
本文介绍了使用expressjs时,下载的.pdf文件已损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 meanjs 应用程序rel =noreferrer > https://github.com/DaftMonk/generator-angular-fullstack 。我试图使用 phantomjs 生成.pdf文件,并将其下载到浏览器。

I am working on meanjs application generated using https://github.com/DaftMonk/generator-angular-fullstack. I am trying to generate a .pdf file using phantomjs and download it to the browser.

问题是下载的.pdf文件总是显示空白页,而不管页数。服务器上的原始文件没有损坏。当我进一步调查时,发现下载的文件总是远远大于磁盘上的原始文件。此问题只发生在.pdf文件中。其他文件类型正常工作。

The issue is that the downloaded .pdf file always shows the blank pages regardless of the number of pages. The original file on server is not corrupt. When I investigated further, found that the downloaded file is always much larger than the original file on the disk. Also this issue happens only with .pdf files. Other file types are working fine.

我尝试过几种方法,如 res.redirect('http:// localhost:9000 / assets / exports / receipt.pdf'); res.download('client\\assets\\\exports\\receipt.pdf')

I've tried several methods like res.redirect('http://localhost:9000/assets/exports/receipt.pdf');, res.download('client\\assets\\exports\\receipt.pdf'),

var fileSystem = require('fs');
var stat = fileSystem.statSync('client\\assets\\exports\\receipt.pdf');
res.writeHead(200, {
                    'Content-Type': 'application/pdf',
                    'Content-Length': stat.size
                });

var readStream = fileSystem.createReadStream('client\\assets\\exports\\receipt.pdf');
return readStream.pipe(res);

甚至我尝试使用 https://github.com/expressjs/serve-static ,结果没有变化。

and even I've tried with https://github.com/expressjs/serve-static with no changes in the result.

我是新的 nodejs 。将.pdf文件下载到浏览器中的最佳方式是什么?

I am new to nodejs. What is the best way to download a .pdf file to the browser?

更新:
我在Windows上运行8.1 64位计算机

Update: I am running this on a Windows 8.1 64bit Computer

推荐答案

我也在服务静态pdf时遇到了腐败。我试过上面提到的一切。然后我发现这个:
https://github.com/intesso/connect- livereload / issues / 39
本质上,通常出色的connect-livereload(包〜0.4.0)正在破坏pdf。
所以只需要通过以下方式忽略pdf:

I had corruption when serving static pdfs too. I tried everything suggested above. Then I found this: https://github.com/intesso/connect-livereload/issues/39 In essence the usually excellent connect-livereload (package ~0.4.0) was corrupting the pdf. So just get it to ignore pdfs via:

app.use(require('connect-livereload')({ignore: ['.pdf']}));

现在这个工程:

app.use('/pdf', express.static(path.join(config.root, 'content/files')));

...很大的救济。

这篇关于使用expressjs时,下载的.pdf文件已损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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