iTextSharp生成PDF:如何将pdf发送到客户端并添加提示?

我已经使用iTextSharp生成了一个pdf,当它创建时,它会自动保存在我的代码中提供的位置,而不是在客户端的服务器上,当然也不会告诉用户任何东西。

我需要将它发送给客户端,我需要提示一个对话框询问用户他想要保存他的pdf的位置。

我该怎么办?

这是我的pdf代码:

using (MemoryStream myMemoryStream = new MemoryStream()) { Document document = new Document(); PdfWriter PDFWriter = PdfWriter.GetInstance(document, myMemoryStream); document.AddHeader("header1", "HEADER1"); document.Open(); //.......... document.Close(); byte[] content = myMemoryStream.ToArray(); // Write out PDF from memory stream. using (FileStream fs = File.Create(HttpContext.Current.Server.MapPath("~\\report.pdf"))) { fs.Write(content, 0, (int)content.Length); } 

编辑

这是我想要http://examples.extjs.eu/?ex=download的结果示例

感谢您的回复,我修改了我的代码:

 HttpContext.Current.Response.ContentType = "application/pdf"; HttpContext.Current.Response.AppendHeader( "Content-Disposition", "attachment; filename=test.pdf"); using (MemoryStream myMemoryStream = new MemoryStream()) { Document document = new Document(); PdfWriter PDFWriter = PdfWriter.GetInstance(document, myMemoryStream); document.AddHeader("Content-Disposition", "attachment; filename=wissalReport.pdf"); document.Open(); //.......... document.Close(); byte[] content = myMemoryStream.ToArray(); HttpContext.Current.Response.Buffer = false; HttpContext.Current.Response.Clear(); HttpContext.Current.Response.ClearContent(); HttpContext.Current.Response.ClearHeaders(); HttpContext.Current.Response.AppendHeader("content-disposition","attachment;filename=" + "my_report.pdf"); HttpContext.Current.Response.ContentType = "Application/pdf"; //Write the file content directly to the HTTP content output stream. HttpContext.Current.Response.BinaryWrite(content); HttpContext.Current.Response.Flush(); HttpContext.Current.Response.End(); 

但我得到这个错误:

 Uncaught Ext.Error: You're trying to decode an invalid JSON String: %PDF-1.4 %     3 0 obj <>stream x   |E  ........... 

我绝对肯定我的itextsharp创建pdf是正确的,因为我可以将它保存在服务器上,但那不是我需要做的,当我尝试将其发送到客户端我得到上面的错误

提前致谢

对于Web应用程序,您可能希望将pdf作为二进制文件流式传输给用户,这将打开pdf或提示用户保存文件。

记住pdf生成正在服务器上进行,即使用户提供了它在服务器上没有任何用处的路径。 请参阅以下链接 –

  • 如何使用ASP.NET和Visual C#.NET将二进制文件写入浏览器

在你的情况下,你正在生成文件,因此已经有一个二进制流而不是文件,因此你可以直接使用Response.BinaryWrite而不是Response.WriteFile 。

修改样本:

 Response.Buffer = false; Response.Clear(); Response.ClearContent(); Response.ClearHeaders(); //Set the appropriate ContentType. Response.ContentType = "Application/pdf"; //Write the file content directly to the HTTP content output stream. Response.BinaryWrite(content); Response.Flush(); Response.End(); 

您需要将内容处置标头发送到用户浏览器。 从内存中代码有点像这样:

 Response.ContentType = "application/pdf"; Response.AppendHeader("Content-Disposition","attachment; filename=nameofthefile.pdf"); 

目前,您将文件保存在文件服务器上,从而在每次请求时覆盖相同的pdf。 如果同时收到两个PDF请求,可能会导致错误。

使用Response将PDF(从内存流)返回给用户,并跳过将PDF写入服务器本地文件的过程。

浏览器将询问用户应保存文件的位置。 就像是:

  Response.ContentType = "Application/pdf"; myMemoryStream.CopyTo(Response.OutputStream); 

另请查看Alun的答案 ,使用content-disposition您可以向用户提出文件名。

解决了

错误来自提交操作,试图解释它不能的响应,因为它不是已知的格式。

我只是设置window.location来下载文件,这很好用。

 { xtype:'button', text: 'Generate PDF', handler: function () { window.location = '/AddData.ashx?action=pdf'; } } 

除了设置位置,您还可以执行window.open()。

是下载还是打开文件取决于浏览器设置。

您不需要使用MemoryStream 。 请改用Response.OutputStream 。 这就是它的用途。 无需使用Response.BinaryWrite()或任何其他调用来显式写入文档; 当您使用Response.OutputStream时,iTextSharp负责写入流。

这是一个简单的工作示例:

 Response.ContentType = "application/pdf"; Response.AppendHeader( "Content-Disposition", "attachment; filename=test.pdf" ); using (Document document = new Document()) { PdfWriter.GetInstance(document, Response.OutputStream); document.Open(); document.Add(new Paragraph("This is a paragraph")); } 

以下是添加正确HTTP标头的方法 。 (获取保存文件的提示)如果您的代码是web form (按钮单击处理程序),则在using语句之后将Response.End()添加到上面的代码示例中,以便不附加Web表单的HTML输出PDF文件。