我需要獲取要保存的電子郵件中的附件,我會根據需要執行閱讀電子郵件和保存的整個程序,但是當我嘗試獲取要保存在 AWS S3 存盤桶中的附件時,會出現錯誤它嘗試讀取內容型別。基本上我需要做的是。
- 獲取附件。
- 為我的 S3 保存功能創建一個 FormFile 以保存附件(S3 保存功能已經在作業)。我在構建 FormFile 時遇到問題。
.
.
.
var attachmentsToS3 = new FormFileCollection();
foreach (var attachment in message.Attachments)
{
var part = attachment as MimePart;
var stream = new MemoryStream();
await part.Content.DecodeToAsync(stream);
var fileLength = stream.Length;
var formFile = new FormFile(stream, 0, fileLength, "file[]", attachment.ContentDisposition.FileName);
attachmentsToS3.Add(formFile);
}
await _atendimentoServices.SaveAnexoAtendimentoToS3( attachmentsToS3, idAcompanhamento, requestApi);
.
.
.
更新
根據布魯諾的回答所做的更改,現在正在運行。
var attachmentsToS3 = new FormFileCollection();
foreach (MimeEntity attachment in message.Attachments)
{
var memory = new MemoryStream();
if (attachment is MimePart part)
await part.Content.DecodeToAsync(memory);
else
await ((MessagePart)attachment).Message.WriteToAsync(memory);
var bytes = memory.ToArray();
var contentType = MimeTypes.GetMimeType(attachment.ContentType.MimeType);
var formFile = new FormFile(memory, 0, bytes.Length, "file[]", attachment.ContentDisposition.FileName)
{
Headers = new HeaderDictionary(),
ContentType = contentType
};
attachmentsToS3.Add(formFile);
}
await _atendimentoServices.SaveAnexoAtendimentoToS3(attachmentsToS3, idAcompanhamento, requestApi);```
uj5u.com熱心網友回復:
下午好,Gabriel,正在分析您的代碼我注意到缺少某些代碼部分。
首先,您需要將您的依戀描述為:
if (attachment is MimePart part) await part.Content.DecodeToAsync(memory);
else await ((MessagePart)attachment).Message.WriteToAsync(memory);
這樣你就可以保證你的流將被正確地填充為 mimeType 或不是。
缺少的另一部分是您的 FormFile 建構式中的標題和內容型別,請嘗試。
var formFile = new FormFile(memory, 0, bytes.Length, "file[]", attachment.ContentDisposition.FileName)
{
Headers = new HeaderDictionary(),
ContentType = MimeTypes.GetMimeType(attachment.ContentType.MimeType);
};
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/458812.html
