use*_*110 3 c# attachment microsoft-graph-api
是否可以通过 Microsoft Graph API 在 C# 中保存文件附件?
我知道我可以获得附件的属性(https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/attachment_get) - 我们是否也可以将其保存到某个地点?
小智 11
一旦您拥有特定的 Microsoft Graph 消息,您就可以将其作为参数传递给方法。然后,您需要发出另一个请求以通过消息 ID 获取附件,迭代附件并将其强制转换为FileAttachment访问ContentBytes属性,最后将此字节数组保存到文件中。
private static async Task SaveAttachments(Message message)
{
var attachments =
await _client.Me.MailFolders.Inbox.Messages[message.Id].Attachments.Request().GetAsync();
foreach (var attachment in attachments.CurrentPage)
{
if (attachment.GetType() == typeof(FileAttachment))
{
var item = (FileAttachment)attachment; // Cast from Attachment
var folder = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
var filePath = Path.Combine(folder, item.Name);
System.IO.File.WriteAllBytes(filePath, item.ContentBytes);
}
}
}
Run Code Online (Sandbox Code Playgroud)