Click here to Skip to main content
15,891,633 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
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.
Posted
Comments
F-ES Sitecore 14-Dec-15 10:00am    
Googling would be a first good step "asp.net show pdf from database"
CHill60 14-Dec-15 10:23am    
first thing[^] that came up with a google search
Sinisa Hajnal 14-Dec-15 11:06am    
Google. And next time do that first.

1 solution

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:
HTML
<a href="yourFile.PDF">Your PDF document</a>


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[^].

[EDIT]

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
 
Share this answer
 
v3
Comments
[no name] 14-Dec-15 13:58pm    
That sounds very complete, a 5.
Sergey Alexandrovich Kryukov 14-Dec-15 14:55pm    
Thank you.
—SA
Member 12206403 15-Dec-15 16:58pm    
Thanks for a detailed explanation on my query. could you please show me some sample code (preferably in VB) for any of your suggestions below?
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

Thanks.
Sergey Alexandrovich Kryukov 15-Dec-15 18:35pm    
Please see the update on my answer, after [EDIT].
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900