How to open PDF file in a new tab or window instead of downloading it (using asp.net)?

前端 未结 7 1710
南方客
南方客 2020-11-28 10:16

This is the code for downloading the file.

System.IO.FileStream fs = new System.IO.FileStream(Path+\"\\\\\"+fileName, System.IO.FileMode.Open, System.IO.File         


        
相关标签:
7条回答
  • 2020-11-28 10:49

    Here I am using iTextSharp dll for generating PDF file. I want to open PDF file instead of downloading it. So I am using below code which is working fine for me. Now pdf file is opening in browser ,now dowloading

            Document pdfDoc = new Document(PageSize.A4, 25, 10, 25, 10);
            PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
            pdfDoc.Open();
            Paragraph Text = new Paragraph("Hi , This is Test Content");
            pdfDoc.Add(Text);
            pdfWriter.CloseStream = false;
            pdfDoc.Close();
            Response.Buffer = true;
            Response.ContentType = "application/pdf";
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.End();
    

    If you want to download file, add below line, after this Response.ContentType = "application/pdf";

    Response.AddHeader("content-disposition", "attachment;filename=Example.pdf");   
    
    0 讨论(0)
提交回复
热议问题