无法访问关闭的流。 System.IO.MemoryStream.Read(byte[] 缓冲区, int 偏移量, int 计数)

问题描述 投票:0回答:1

我正在尝试创建 pdf 文件,然后从字节数组添加图像 但我收到了这个错误

  • 处理请求时发生未处理的异常。 ObjectDisposeException:无法访问关闭的流。 System.IO.MemoryStream.Read(byte[] 缓冲区, int 偏移量, int 计数)

我附上图片描述了我的尝试my code

c# asp.net asp.net-core itext
1个回答
0
投票

正如我的评论中提到的 doc.close 正在关闭你的内存流。所以在关闭之前获取字节。

 // Before closing the document, retrieve the bytes
    byte[] bytes = ms.ToArray();

    doc.Close(); // This will close the MemoryStream `ms`

    // Create a new MemoryStream with the bytes of the closed MemoryStream
    var resultStream = new MemoryStream(bytes);

    // Return a FileStreamResult with the new MemoryStream
    return new FileStreamResult(resultStream, "application/pdf");
© www.soinside.com 2019 - 2024. All rights reserved.