Opening a PDF in browser instead of downloading it

43,409

Solution 1

Change the content-disposition to inline instead of attachment.

The second line of your snippet would then be

Response.AddHeader("content-disposition", "inline;filename=" + filename + ".pdf");

See Content-Disposition:What are the differences between "inline" and "attachment"? for further details.

Solution 2

Try This Code :

Action:

Response.AddHeader("content-disposition", "attachment;filename=" + filename + ".pdf");

To open in new Tab/Window:

@Html.ActionLink("view pdf", "getpdf", "somecontroller", null, 
                  new { target = "_blank" })

OR

<a href="GeneratePdf.ashx?somekey=10" target="_blank">

Solution 3

You should look at the "Content-Disposition" header; for example setting "Content-Disposition" to "attachment; filename=FileName.pdf" will prompt the user (typically) with a "Save as: FileName.pdf" dialog, rather than opening it. This, however, needs to come from the request that is doing the download, so you can't do this during a redirect. However, ASP.NET offers Response.TransmitFile for this purpose. For example (assuming you aren't using MVC, which has other preferred options):

Response.Clear();
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "attachment; filename=FileName.pdf");
Response.TransmitFile(Server.MapPath("~/folder/Sample.pdf"));
Response.End(); 

If you are try to open then the file in apicontroller Convert stream to bytesarray and then Fill the content

HttpResponseMessage result = null;
result = Request.CreateResponse(HttpStatusCode.OK);
FileStream stream = File.OpenRead(path);
byte[] fileBytes = new byte[stream.Length];
stream.Read(fileBytes, 0, fileBytes.Length);
stream.Close();           
result.Content = new ByteArrayContent(fileBytes);
result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
result.Content.Headers.ContentDisposition.FileName = "FileName.pdf";            

I think it will help you...

Share:
43,409

Related videos on Youtube

Krishna Thota
Author by

Krishna Thota

I'm a .Net Developer Working in Hyderabad. I have Knowledge on .NET, SQL Server, and other .Net Extensions

Updated on March 14, 2020

Comments

  • Krishna Thota
    Krishna Thota over 4 years

    I'm using iTextSharp to print a panel into PDF on button click. After clicking on the button, the PDF is downloading to the client's computer. Instead of this I need the PDF to be opened in a browser instead of downloading. From the browser the user will be able to download the PDF to his PC.

    I'm using the following code:

    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=" + filename + ".pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    pnl_print.RenderControl(hw);
    
    StringReader sr = new StringReader(sw.ToString());
    Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
    HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
    pdfDoc.Open();
    htmlparser.Parse(sr);
    pdfDoc.Close();
    Response.Write(pdfDoc);
    Response.End();
    
    sr.Close();
    hw.Close();
    sw.Close();
    
  • Krishna Thota
    Krishna Thota almost 12 years
    Thank you It helped me. Can you help me in this. applying-styles-in-pdf-gridview-using-itextsharp
  • Alexis Pigeon
    Alexis Pigeon almost 12 years
    @KrishnaThota I would love to, but unfortunately I don't have the answer for that one.
  • Krishna Thota
    Krishna Thota almost 12 years
    There is a label inside the panel which I'm making as PDF the Label is not being Displayed in PDF Can you help me?
  • Krishna Thota
    Krishna Thota almost 12 years
    How can I Open the Pdf file in New tab instead of opening it in the same tab?
  • Alexis Pigeon
    Alexis Pigeon almost 12 years
    As mentionned in another answer, in order to open the PDF in another tab/window, you'll have to set the target of the link to "_blank".
  • Alexis Pigeon
    Alexis Pigeon almost 12 years
    Shouldn't the content-dispositon be inline?
  • Krishna Thota
    Krishna Thota almost 12 years
    Where should i write the @Html. thing?
  • Krishna Thota
    Krishna Thota almost 12 years
    I'm using asp:image button not <a/> tag
  • Krishna Thota
    Krishna Thota almost 12 years
    I'm using asp:image button not <a/> tag
  • Krunal Mevada
    Krunal Mevada almost 12 years
    insted image button of asp you have to user anchor tag <a><img /></a> like this
  • Krishna Thota
    Krishna Thota almost 12 years
    I'm not getting you href="GeneratePdf.ashx?somekey=10" What is this line? I'm not generating pdf from ashx file! I'm generating the pdf from the same page
  • Krishna Thota
    Krishna Thota almost 12 years
    Can you please explain I'm a new bi. The Second and third lines of code you said are HTML code not C# code. so how can I write that code then the PDF populates?? The Data will be populated in the PDF between Doccument.open() and doccument.close() in the Serverside (aspx.cs) file.
  • Krunal Mevada
    Krunal Mevada almost 12 years
    you put your code at some button event or else i want to know that.. mention html code is for mvc
  • sandeep
    sandeep over 10 years
    target='_blank' is not working for me, Its opening a blank new tab, and the pdf is opening in the same tab. Can anyone help me out to open the pdf in a new tab. Thanks
  • Alexis Pigeon
    Alexis Pigeon over 10 years
    @sandeep you should create your own question, with more detail, snippets of your code, etc...
  • sandeep
    sandeep over 10 years
    @Alexis Pigeon : I was thinking of to create my own question, but fortunately i found this thread which worked for me. The only thing which not working is "target=_blank". People might mark it as duplicate if i ask the same question again
  • Alexis Pigeon
    Alexis Pigeon over 10 years
    @sandeep Explain then in your question that you tried the solutions proposed here, but that you don't manage to get the target='blank' thing working. People should not mark it as a duplicate then (in theory...)