使用express js在浏览器中显示Pdf [英] Display Pdf in browser using express js

查看:422
本文介绍了使用express js在浏览器中显示Pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

app.post('/asset', function(request, response){
  var tempFile="/home/applmgr/Desktop/123456.pdf";
  fs.readFile(tempFile, function (err,data){
     response.contentType("application/pdf");
     response.send(data);
  });
});

我是一个新的表达式,我不能发送与数据对象的响应。
二进制内容在浏览器中可见。
给我建议如何处理这个?

I am a new bie to expressjs, I can't send the response with the data object. The binary content is visible in the browser. Give me suggestions how to handle this ?

推荐答案

我测试了你的代码,它适用于我,更改:
app.post 更改为 app.get

I tested your code and it works for me in chrome with one change: Change app.post to app.get

编辑:由于您似乎认为仅POST服务器是个好主意,请阅读: http://net.tutsplus.com/tutorials/other/a-beginners-introduction-to-http-and-rest/
向下滚动直到HTTP动词,并查看GET和POST之间的区别。 :)

since you seem to think a POST-only server is a good idea, read this: http://net.tutsplus.com/tutorials/other/a-beginners-introduction-to-http-and-rest/ Scroll down until the HTTP verbs and check out the difference between GET and POST. :)

一些快速研究表明,其他浏览器可能还有其他问题,例如IE可能希望URL以 .pdf 。因为我在我的Mac我无法为你测试;)

Some quick research suggests that other browsers might have other issues, IE for example might expect the URL to end in .pdf. Since I'm on my Mac I can't test that for you ;)

这篇关于使用express js在浏览器中显示Pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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