dotnetrocks
dotnetrocks

Reputation: 2727

Save download file dialog

I have created a PDF using iText and storing it in a particular location (specified in the code). I would like to prompt a save dialog box for the user to choose location on his computer to save the pdf. I checked iText tutorial but it didn't help me.

Here is the code for generating the PDF file:

Document objDoc = new Document();
PdfWriter.GetInstance(objDoc, new FileStream("C:\\HelloWorld.pdf", FileMode.Create));
objDoc.Open();
objDoc.Add(new Paragraph("welcome iText Pdf"));
objDoc.Close();

I tried like this for saving:

string FileName ="HelloWorld.pdf";
String FilePath = @"C:\";
HttpResponse response = HttpContext.Current.Response;
response.ClearContent();
response.Clear();
response.ContentType = "application/pdf";
response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ";");
response.TransmitFile(FilePath + FileName);
response.Flush();
response.End();

Upvotes: 0

Views: 2417

Answers (1)

Richard
Richard

Reputation: 30628

I'm assuming you're doing this from a web page since you tagged this ASP.NET. You need to add the Content-Disposition header. See the following question for details:

Force download of a file on web server - ASP .NET C#

Upvotes: 1

Related Questions