0

I am using iTextSharp to create a PDF file (I use C#). The file is created fine; however, it is saved in a folder. The file is created in response to a link button click and I want to display the Open/Save dialog box rather than save the fie in a folder. I use the following:

using(MemoryStream ms = new MemoryStream())
{
    Document doc1 = new Document();
    PdfWriter pdfw = PdfWriter.GetInstance(doc1, ms);
    doc1.open();
    string sPath = Server.MapPath("PDF/");
    string sFileName = "Something.PDF";

    // create the PDF content

    doc1.Close();
    byte[] content = ms.ToArray();
    using (FileStream fs = FileStream.Create(sPath + sFileName))
    {
        fs.Write(content, 0, (int)content.Length);
    }
}

Am I doing this wrong? It creates the file and saves it in "blahblah/PDF" folder but does not display Open/Save dialog bx.

NoBullMan
  • 2,032
  • 5
  • 40
  • 93

1 Answers1

1

If you already have the file, use Response.TransmitFile method to flush it in the user's browser.

Response.Clear(); 
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "attachment; filename=buylist.pdf");
Response.TransmitFile(Server.MapPath("~/myfile.pdf"));
VahidN
  • 18,457
  • 8
  • 73
  • 117
  • Thanks Vahid. Is there any way to do this without saving the file to a folder first? I wonder what happens if 100 users click on the link to create the PDF file at the same time, using different criteria to generate the data. – NoBullMan Aug 23 '13 at 15:10
  • 1
    Instead of `Response.TransmitFile()` you can use `Response.BinaryWrite()` and pass your byte array directly to that. – Chris Haas Aug 23 '13 at 18:18
  • Excellent! That solved it. It displays Open/Save without requiring to write the file to a folder first. Vahid's suggestion pointed me in the right direction and you change finalized it. – NoBullMan Aug 24 '13 at 03:05