MVC。 Itextsharp 将 pdf 写入响应

发布于 2024-09-25 17:13:05 字数 955 浏览 2 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

左耳近心 2024-10-02 17:13:05

你能不能不使用

Response.ContentType = "application/pdf"
Response.AddHeader("Content-Type", "application/pdf")
Response.WriteFile(pdfFilePath)
Response.End()

Could you not use

Response.ContentType = "application/pdf"
Response.AddHeader("Content-Type", "application/pdf")
Response.WriteFile(pdfFilePath)
Response.End()
欢你一世 2024-10-02 17:13:05

您应该使用 FileContentResult Controller.File(byte[] content, string contentType) 方法:

public ActionResult GeneratePDF()
{
    var outputStream = new MemoryStream(); // This will hold the pdf you want to send in the response

    /*
     * ... code here to create the pdf in the outputStrem
     */

    return File(outputStream.ToArray(), "application/pdf");
}

来源:在 Asp.Net MVC 2 中构建 PDF

You should use the FileContentResult Controller.File(byte[] content, string contentType) method:

public ActionResult GeneratePDF()
{
    var outputStream = new MemoryStream(); // This will hold the pdf you want to send in the response

    /*
     * ... code here to create the pdf in the outputStrem
     */

    return File(outputStream.ToArray(), "application/pdf");
}

Source: Building PDFs in Asp.Net MVC 2.

凡间太子 2024-10-02 17:13:05

内存流可能仍然设置在最后写入字节之后的位置。它将从当前位置写入所有字节(没有)。如果您执行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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文