iTextSharp: Display “Open/Save” dialog box when PDF file is created

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.

Answers


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"));

Need Your Help

WCF - Service Contract from another assembly

c# wcf assemblies

I have tried googling for an answer since last week and haven't found anything. Maybe I'm just searching with incorrect key words...

Best Database Change Control Methodologies

sql database architecture schema methodology

As a database architect, developer, and consultant, there are many questions that can be answered. One, though I was asked recently and still can't answer good, is...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.