I've got a project where I'm using a PDF generator to send a file to a user. We'd like to give the user the option to attach this file to an email instead, and we're having trouble using the Stream object and Attachment logic together.
我有一个项目,我正在使用PDF生成器向用户发送文件。我们希望为用户提供将此文件附加到电子邮件的选项,而我们在使用Stream对象和附件逻辑时遇到问题。
We start with ABCpdf, which has two save methods: it can save to a Stream, or if you give it a string, it'll try to save to a file on the disk there. We've done both no problem.
我们从ABCpdf开始,它有两个保存方法:它可以保存到Stream,或者如果你给它一个字符串,它会尝试保存到磁盘上的文件。我们两个都没问题。
Stream stream = new MemoryStream();
myPdf.Save(stream);
Everything is mostly cool at this point - stream
has several kilobytes of data, and if you .Save() to a file, you get an actual file with the same number of bytes.
此时一切都很酷 - 流有几千字节的数据,如果你.Save()到一个文件,你得到一个具有相同字节数的实际文件。
So we attach to an email at this point (after initializing the mail object, setting To: and From:, etc.):
所以我们此时附加了一封电子邮件(在初始化邮件对象后,设置为:和From:等等):
mail.Attachments.Add(new Attachment(stream, "myPdf.pdf"));
mail.Send();
...which gets us as far as receiving an email with 0 bytes, but the proper filename.
...这使我们收到一个0字节的电子邮件,但正确的文件名。
All the examples I'm finding online use a StreamReader or a StreamWriter or a Flush() or something. It always seems like it's more complicated than simply passing a Stream, but maybe only one or two lines more complicated. None of those examples start with a Stream - they're always trying to turn an array into a Stream to show you how easy that is, or grab a file from disk (which we can't do, which is why we're excited to use a Stream).
我在网上找到的所有例子都使用StreamReader或StreamWriter或Flush()等。它似乎总是比传递一个Stream更复杂,但也许只有一两行更复杂。这些示例都不是以Stream开头的 - 他们总是试图将数组转换为Stream来向您展示它是多么容易,或者从磁盘中获取文件(我们无法做到,这就是为什么我们很兴奋使用Stream)。
Anyway, if anyone can explain what I'm doing wrong or what I ought to be doing, I'd really appreciate it. Thanks.
无论如何,如果有人能解释我做错了什么或我应该做什么,我真的很感激。谢谢。
2 个解决方案
#1
35
A guess... Back dat stream up to the beginning before sending nit
猜测......在发送nit之前将数据流返回到开头
// Set the position to the beginning of the stream.
stream.Seek(0, SeekOrigin.Begin);
mail.Attachments.Add(new Attachment(stream, "myPdf.pdf"));
mail.Send();
#2
-1
Have you tried saving the file and then attaching the file, rather than attempting to attach a stream?
您是否尝试过保存文件然后附加文件,而不是尝试附加流?
added
添加
The normal process here would be
这里的正常过程是
-
Use the stream to create a file on disk
使用流在磁盘上创建文件
-
Attach the file to an email
将文件附加到电子邮件中
-
Delete the file when done with it.
完成后删除文件。
#1
35
A guess... Back dat stream up to the beginning before sending nit
猜测......在发送nit之前将数据流返回到开头
// Set the position to the beginning of the stream.
stream.Seek(0, SeekOrigin.Begin);
mail.Attachments.Add(new Attachment(stream, "myPdf.pdf"));
mail.Send();
#2
-1
Have you tried saving the file and then attaching the file, rather than attempting to attach a stream?
您是否尝试过保存文件然后附加文件,而不是尝试附加流?
added
添加
The normal process here would be
这里的正常过程是
-
Use the stream to create a file on disk
使用流在磁盘上创建文件
-
Attach the file to an email
将文件附加到电子邮件中
-
Delete the file when done with it.
完成后删除文件。