MVC。 Itextsharp 将 pdf 写入响应
我正在使用 itexsharp 生成 pdf。 我正在创建 MemoryStream,然后当我尝试将 MemoryStream 字节写入响应但没有运气。当我在控制器中执行此代码时,pdf 没有响应。内存流填充正确,我可以在调试器中看到这一点,但由于某种原因,这个数量的比特没有响应。
这是我的代码:
HttpContext.Current.Response.ContentType = "application/pdf";
...
using (Stream inputPdfStream = new FileStream(pdfFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
using (Stream outputPdfStream = new MemoryStream())
{
PdfReader reader = new PdfReader(inputPdfStream);
PdfStamper stamper = new PdfStamper(reader, outputPdfStream);
....
//try one
outputPdfStream.WriteTo(HttpContext.Current.Response.OutputStream); // NOT POPULATING Response
//try two
HttpContext.Current.Response.BinaryWrite(outputPdfStream.ToArray()); // NOT POPULATING Response Too
HttpContext.Current.Response.End();
}
可能有人有什么想法吗?
I am generating pdf using itexsharp.
I am creating MemoryStream, then when i am trying t write MemoryStream bytes in to response but no luck. When i am executing this code in my controller the pdf not coming in response. Memory stream is populaitng correctly i can see this in debugger, but for some reason this number of butes not coming in response.
Here is my code:
HttpContext.Current.Response.ContentType = "application/pdf";
...
using (Stream inputPdfStream = new FileStream(pdfFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
using (Stream outputPdfStream = new MemoryStream())
{
PdfReader reader = new PdfReader(inputPdfStream);
PdfStamper stamper = new PdfStamper(reader, outputPdfStream);
....
//try one
outputPdfStream.WriteTo(HttpContext.Current.Response.OutputStream); // NOT POPULATING Response
//try two
HttpContext.Current.Response.BinaryWrite(outputPdfStream.ToArray()); // NOT POPULATING Response Too
HttpContext.Current.Response.End();
}
May be some one have any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你能不能不使用
Could you not use
您应该使用
FileContentResult Controller.File(byte[] content, string contentType)
方法:来源:在 Asp.Net MVC 2 中构建 PDF。
You should use the
FileContentResult Controller.File(byte[] content, string contentType)
method:Source: Building PDFs in Asp.Net MVC 2.
内存流可能仍然设置在最后写入字节之后的位置。它将从当前位置写入所有字节(没有)。如果您执行
outputPdfStream.Seek(0)
,它将把位置设置回第一个字节,并将整个流的内容写入响应输出。不管怎样,就像 Dean 说的,你应该只使用 Reponse.WriteFile 方法。
Probably the memorystream is still set at the position after the last written byte. It will write all bytes from the current position (which is none). If you do a
outputPdfStream.Seek(0)
it will set the position back to the first byte, and will write the contents of the whole stream to the response output.Anyway, like Dean says, you should just use the
Reponse.WriteFile
method.