Frage

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.

War es hilfreich?

Lösung

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"));
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top