微软图表 - 如何通过C#保存文件附件?

3
2个回答

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);
        }
    }
}

2
当您获取附件属性时,它们将包含有关附件的信息。
三种类型的附件。
首先,在属性的@odata.type中检查附件类型,并相应地处理它们。
对于fileAttachment类型,它们包含一个contentLocation属性,该属性是附件内容的 URI
您可以从URI下载附件。

你好, 在我的情况下,contentLocation是NULL。据我所知,@Tomas的答案是一种替代方案,其中我们保存contentbytes。如何在Javascript中实现相同的操作? - Irfan Harun

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接