通过 FileStreamResult 获取 EmailMessage 附件

发布于 2024-09-13 11:10:05 字数 624 浏览 3 评论 0原文

我在这里有这段代码,我使用 EWS 从 Exchange Server 上的电子邮件消息中检索附件

            Attachment attachment = message.Attachments.Single(att => att.ContentId == Request.QueryString["cid"]);
            attachment.Load();
            FileAttachment fileAttachment = attachment as FileAttachment;


            fileAttachment.Load();
            byte[] bytes = fileAttachment.Content;
            Stream theMemStream = new MemoryStream();

            theMemStream.Write(bytes, 0, bytes.Length);

            return new FileStreamResult( theMemStream, attachment.ContentType);

我可以很好地下载该文件,但是它们已损坏...我缺少什么吗?

I have this code here where I retrieve an attachment from an Email Message that is on the Exchange Server using EWS

            Attachment attachment = message.Attachments.Single(att => att.ContentId == Request.QueryString["cid"]);
            attachment.Load();
            FileAttachment fileAttachment = attachment as FileAttachment;


            fileAttachment.Load();
            byte[] bytes = fileAttachment.Content;
            Stream theMemStream = new MemoryStream();

            theMemStream.Write(bytes, 0, bytes.Length);

            return new FileStreamResult( theMemStream, attachment.ContentType);

I can download the file just fine however they are corrupted... Is there something I'm missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

乖乖 2024-09-20 11:10:05

您可以直接使用 FileContentResult相反 - 这样您就不必通过 MemoryStream。这样,您破坏任何东西的风险就会降低。

return FileContent(fileAttachment.Content, attachment.ContentType);

如果您希望文件在浏览器中内联显示,您可能还需要设置FileDownloadName

You can use a FileContentResult directly instead - that way you don't have to go via a MemoryStream. That way, you have less risk of breaking anything.

return FileContent(fileAttachment.Content, attachment.ContentType);

You might also want to set the FileDownloadName if you don't want the file to display inline within the browser.

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