通过FileStreamResult获取EmailMessage附件

时间:2010-08-06 05:39:09

标签: asp.net-mvc asp.net-mvc-2 exchange-server exchangewebservices ews-managed-api

我这里有这段代码,我从使用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);

我可以下载该文件,但它们已损坏...是否有我遗失的东西?

1 个答案:

答案 0 :(得分:2)

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

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

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

相关问题