Click here to Skip to main content
15,893,622 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have do something like this but..its not working..for open... get downloaded..

C#
protected void LinkButton1_Click(object sender, EventArgs e)
        {

            string path = Server.MapPath("Attachments/ListAwardedClassX.pdf");
            WebClient client = new WebClient();
            Byte[] buffer = client.DownloadData(path);

            if (buffer != null)
            {
                Response.ContentType = "application/pdf";
                Response.AddHeader("content-length",  
    buffer.Length.ToString());
                Response.BinaryWrite(buffer);
            }
        }
Posted
Comments
Dave Kreskowiak 7-Jun-15 21:01pm    
If this is ASP.NET code, why are you creating a WebClient to download something from a local file? This is a very strange thing to do.

There is a requirement on the client side to display a PDF. The client must have some kind of PDF viewer installed and registered in order for it to display. If there is no registered handler for the mime type, the file automatically gets downloaded.
Sergey Alexandrovich Kryukov 7-Jun-15 21:19pm    
Of course. Please see my answer where I try to argue in favor of this idea.
—SA

1 solution

Before discussing anything, I'll make some statement which some may persist as a joke: the most sensible way is this:
HTML
<a href="ListAwardedClassX.pdf">This is your PDF document</a>

Because 1) PDF is not a part of PDF standards; 2) PDF of very popular, so most system "know" how to read and show it.

The second approach would be to represent PDF as HTML and show it on the page, but I would not recommend it.

Now I can explain. First of all, PDF and HTML cannot adequately represent documents created using these two different standards. Their concepts are very different: PDF document is totally fixed, something like a "digital paper" document; and, in contrast, HTML is fluid, its layout is supposed to adopt to any window size. It's not possible to represent PDF document on the HTML in 100% adequate way, something will always be lost.

At the same time, most users will have PDF support in their browsers, with one or another plug-ins; all those plug-ins are non-standard extension. If such thing is not available, the browser will suggest to download the document. Finally, a system may or may not have anything to view the document. The Web developer should make no assumption on what the user has. If there is nothing to view it, it's absolutely not a problem; the user can always save the document and view it later, or on some other system. In other words, the viewing of the document should be left to the user; you only provide an anchor.

—SA
 
Share this answer
 

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