如何在客户端浏览器中显示pdf文件 [英] How can I display a pdf file in client browser

查看:220
本文介绍了如何在客户端浏览器中显示pdf文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我是asp.net编程的初学者。有人请指导我如何在客户端borwser中显示pdf文件?我将pdf文件存储在数据库(sql)中作为二进制文件。我需要帮助从数据库中检索二进制文件,并在单击按钮时在浏览器中显示该文件。我使用VB作为编程语言。



请尽可能给我一个解决方案。



谢谢,

Suresh。

Hi,

I am a beginner in asp.net programming. Anyone please guide me how can i display a pdf file in the client borwser? i have pdf files stored in database (sql) as binary file. I need help in retrieving the binary files from database and display the file in browser on a button click. i am using VB as the programming language.

Please give me a solution if possible.

Thanks,
Suresh.

推荐答案

首先要明白的是:PDF不是W3标准的一部分(我觉得它很好),所以你不能做任何事情来显示PDF文件,它仍然是客户端部分的问题。一些浏览器将被设置为在浏览器中显示PDF作为页面(通常,使用某些浏览器插件),有些则不会。你不应该对此做出任何假设。您不能假设任何与PDF相关的软件甚至存在于客户端系统上。这根本不是问题。根据浏览器设置,将显示PDF文件或建议用户保存文件,以便用户以后可以随时打开。



存储PDF数据库中的文件不是最佳选择,因为从数据库的角度来看,它是非结构化的,因此您需要将其存储为blob。在网站设置中,通常最好根据一个或另一个唯一名称系统将文件存储在服务器主机的文件系统中,站点目录中;哪个更好,应该是简单的文件名或相对路径。在这种方法中,数据库将只存储文件名。



将文件本身存储在数据库中也是一种选择,我不建议这样做。它与存储任何其他文件没有什么不同,例如图片,音频,视频文件和其他媒体文件等等。一次又一次地回答相关问题是不好的;之前回答的次数太多了: http://www.codeproject.com/search.aspx?q=%28%22。 NET%22 + OR +%22C%23%22%29 +数据库+%28image + OR +图像+ OR +位图+ OR +位图%29&安培; doctypeid = 1%3B2%3B3%3b13%3b14 [<一个HREF =http://www.codeproject.com/search.aspx?q=%28%22.NET%22+OR+%22C%23%22%29+database+%28image+OR+images+OR+bitmap+ OR +位图%29& doctypeid = 1%3b2%3b3%3b13%3b14target =_ blanktitle =新窗口> ^ ]。



现在,如果只存储文件名;并且文件位于您站点的目录中,将文件传递给用户所需的只是生成这样的HTML片段:

First thing to understand: PDF is not a part of W3 standards (and I think it's good), so you cannot do anything to show a PDF file, it remains the problem of client part. Some browsers will be set up to show PDF as pages in browser (usually, with some browser plug-in), some won't. You should not make any assumptions on that. You cannot assume that any PDF-related software even exists on the client system. And that is not a problem at all. Depending on browser settings, PDF file will be shown or the user will be suggested to save the file, so the user can always open it later.

Storing of the PDF files in database is not the best option, because, from the database standpoint, it is something unstructured, so you would need to store it as a blob. In Web site settings, it's usually better to store files in the file system of the server's host, in the directory of the site, based on one or another system of unique names; which better should be simple file names, or relative paths. In this approach, the database will store just the file names.

Storing the files themselves in the database is also an option, which I did not recommend. It's no different from storing any other files, such as pictures, audio, video files and other media files, and a lot more. It would be bad to answer related question again and again; it was answered too many times before: http://www.codeproject.com/search.aspx?q=%28%22.NET%22+OR+%22C%23%22%29+database+%28image+OR+images+OR+bitmap+OR+bitmaps%29&doctypeid=1%3b2%3b3%3b13%3b14[^].

Now, if you store just the file names; and the files are in your site's directory, all you need to deliver the file to the user is to generate HTML fragment like this:
<a href="yourFile.PDF">Your PDF document</a>





如果您将文件存储在数据库中,而不仅仅是文件名(再次,我不推荐它),有两个选择:1)创建一个临时文件并引用它; 2)具有ASP.NET页面,该页面在HTTP响应中动态提供文件内容;您可以在链接锚中引用此页面或以任何其他方式请求。在第二种情况下,最重要的是生成正确的 Content-type HTTP标头,该标头应为application / pdf。内容类型由IANA标准化和维护;请参阅:

https://www.iana.org/assignments /media-types/media-types.xhtml [ ^ ];

另见: https://en.wikipedia。 org / wiki / Media_type [ ^ ]

https://en.wikipedia.org/wiki/Internet_Assigned_Numbers_Authority [ ^ ],

https://en.wikipedia.org/wiki/MIME [ ^ ]。







另外,回答评论中提到的后续问题答案:

选项#1中没有显示任何内容。就这样做。

对于#2,请参阅,例如,

https://msdn.microsoft.com/en-us/library/system.web.httpresponse.contenttype%28v=vs.110%29 .aspx

https://msdn.microsoft.com/en-us/library/system.web.httpresponse%28v=vs.110%29.aspx



这些MSDN页面中显示的代码示例还说明了如何生成引用文件的HTML代码。 保存文件真的没什么,通常的文件操作;这一切都取决于你获得PDF文件的位置。或者,您可以使用适当的ASP.NET语法替换硬编码的文件名,例如Razor http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx [ ^ ]。



这取决于关于如何使用ASP.NET,查看引擎。我真的希望这就是你所需要的,因为解释更多细节需要回答如何使用ASP.NET的问题。 :-)



-SA



If you store files in the database, not just file names (again, I would not recommend it), you have two options: 1) create a temporary file and reference it; 2) have the ASP.NET page which provides the file content dynamically in HTTP response; you can reference this page in the link anchor or request in any other way. In second case, most important thing is to generated correct Content-type HTTP header, which should be "application/pdf". Content types are standardized and maintained by IANA; please see:
https://www.iana.org/assignments/media-types/media-types.xhtml[^];
see also: https://en.wikipedia.org/wiki/Media_type[^]
https://en.wikipedia.org/wiki/Internet_Assigned_Numbers_Authority[^],
https://en.wikipedia.org/wiki/MIME[^].



In addition, to reply the follow-up question asked in the comments to this answer:
There is nothing to show in option #1. Just do it.
For #2, see, for example,
https://msdn.microsoft.com/en-us/library/system.web.httpresponse.contenttype%28v=vs.110%29.aspx,
https://msdn.microsoft.com/en-us/library/system.web.httpresponse%28v=vs.110%29.aspx.

The code samples shown in these MSDN pages also explains how to generate the HTML code referencing a file. "Save file" is really nothing, the usual file operation; it all depends on where you get a PDF file. Or you can replace the hard-coded file name with appropriate ASP.NET syntax, such as Razor http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx[^].

It depends on how you use ASP.NET, what view engine. I really hope this is all you need, because explaining further detail would take as much as answering the question "how to use ASP.NET". :-)

—SA


这篇关于如何在客户端浏览器中显示pdf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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