简体   繁体   中英

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

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#

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM