下载.pdf文件正在使用前pressjs时损坏 [英] Downloaded .pdf files are corrupted when using expressjs

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

问题描述

我在 meanjs 应用程序生成的工作使用<一个href=\"https://github.com/DaftMonk/generator-angular-fullstack\">https://github.com/DaftMonk/generator-angular-fullstack.我想用生成一个.pdf文件 phantomjs ,并将其下载到浏览器。

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://本地主机:9000 /资产/出口/ receipt.pdf'); res.download('\\\\客户资产\\\\ \\\\出口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/ex$p$pssjs/serve-尝试静态与结果没有改变。

我是新来的的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

推荐答案

下面是为从前preSS文件干净的方式,并使用附件头,以确保文件被下载:

Here is a clean way to serve a file from express, and uses an attachment header to make sure the file is downloaded :

var path = require('path');
var mime = require('mime');

app.get('/download', function(req, res){
  //Here do whatever you need to get your file
  var filename = path.basename(file);
  var mimetype = mime.lookup(file);

  res.setHeader('Content-disposition', 'attachment; filename=' + filename);
  res.setHeader('Content-type', mimetype);

  var filestream = fs.createReadStream(file);
  filestream.pipe(res);
});

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

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