MemoryStream的,无法访问已关闭的流 [英] MemoryStream, Cannot access a closed stream
问题描述
通过在 sharpPDF 库我生成PDF内存流,我想它直接通过电子邮件发送。但该行ms.Seek(....给出的ObjectDisposedException;
无法访问已关闭的流。
该pdf.CreatePDF方法采用要么是(输出)文件名字符串或(出)流。但我想这也关闭了流?我不习惯工作,多以流,所以,如果你能请指教应该怎么做呢?
在CreatePDF方法的sharpPDF源$ C $ C可以在这里找到:
<一个href="http://www.java2s.com/Open-Source/CSharp/PDF/SharpPDF/sharpPDF/pdfDocument.cs.htm">http://www.java2s.com/Open-Source/CSharp/PDF/SharpPDF/sharpPDF/pdfDocument.cs.htm
公用Sub SendPDF()
昏暗的PDF作为新sharpPDF.pdfDocument(标题,作者)
....生成PDF内容
昏暗的MS作为新IO.MemoryStream
pdf.CreatePDF(毫秒)
昏暗的电子邮件作为新EmailService
email.Send(毫秒)
结束小组
公共类EmailService
公用Sub发送(BYVAL毫秒为流)
ms.Seek(0,IO.SeekOrigin.Begin)
昏暗的ATC作为新的附件(MS,Report.pdf)
mail.Attachments.Add(ATC)
....设置其他电子邮件参数
client.SendAsync(邮件,mail.Subject)
结束小组
末级
一个简单的方法是让字节数组走出封闭的MemoryStream,创造一个又一个:
pdf.CreatePDF(MS)
MS =新的MemoryStream(ms.ToArray())
昏暗的电子邮件作为新EmailService
email.Send(毫秒)
请注意,它的精细叫<一href="http://msdn.microsoft.com/en-us/library/system.io.memorystream.toarray.aspx"><$c$c>MemoryStream.ToArray$c$c>对的MemoryStream
封闭/处置实例。它甚至记载:
注意
当MemoryStream的关闭,此方法有效。
With the sharpPDF library I generate a pdf memory stream, and I want to send it directly via email. But the line ms.Seek(.... gives an ObjectDisposedException;
Cannot access a closed Stream.
The pdf.CreatePDF method takes either an (output) fileName string, or an (out)Stream. But I guess it also closes the stream? I'm not used to work much with streams, so if you could please advise how it should be done?
The sharpPDF source code of the CreatePDF method can be found here:
http://www.java2s.com/Open-Source/CSharp/PDF/SharpPDF/sharpPDF/pdfDocument.cs.htm
Public Sub SendPDF()
Dim pdf As New sharpPDF.pdfDocument("Title", "Author")
'....Generate pdf content
Dim ms As New IO.MemoryStream
pdf.CreatePDF(ms)
Dim email As New EmailService
email.Send(ms)
End Sub
Public Class EmailService
Public Sub Send(Byval ms as Stream)
ms.Seek(0, IO.SeekOrigin.Begin)
Dim atc As New Attachment(ms, "Report.pdf")
mail.Attachments.Add(atc)
'....set other email parameters
client.SendAsync(mail, mail.Subject)
End Sub
End Class
One simple approach is to get the byte array out of the closed MemoryStream and create another one:
pdf.CreatePDF(ms)
ms = new MemoryStream(ms.ToArray())
Dim email As New EmailService
email.Send(ms)
Note that it's fine to call MemoryStream.ToArray
on a closed / disposed instance of MemoryStream
. It's even documented:
Note
This method works when the MemoryStream is closed.
这篇关于MemoryStream的,无法访问已关闭的流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!