使用 UWP 中的 Microsoft Graph API 获取附加文档列表并将它们下载到本地存储
Getting a list of attached documents and downloading them into local storage using Microsoft Graph API in UWP
我正在使用 Microsoft Graph
API 开发 UWP 应用程序。我正在使用以下 API
获取 logged in
用户当天的会议列表
https://graph.microsoft.com/v1.0/me/calendarView?startDateTime=2017-06-20T20:00:00.0000000&endDateTime=2017-06-21T10:00:00.0000000
创建会议时,我在邀请函中附加了 document
给受邀参与者。
JSON
收到的响应有 "hasAttachments": true,
。我的要求是下载邀请中发送的文件。
我需要使用我的应用程序下载这些文件,然后附加它们并将其发送到 participants
。我该怎么做?
My requirement is to download the file that were send in the invite.
您似乎正在使用 List calendarView API to get the events. In that case you should be able get the event "id" property of the Event 您想要从响应中获得的资源,请检查 "id": "string (identifier)"
。
之后,您可以通过List attachments API, and get a special one attachment by Get attachment. You could get the binary contents of the attached file by contentBytes
property which contains the base64-encoded contents of the file. If your attachment is fileAttachment resource type. For example, if the attachment is a .txt
file, you can download it and save it in application local folder获取本次活动的所有附件,如下:
HttpClient client = new HttpClient();
string token =await AuthenticationHelper.GetTokenForUserAsync();
client.DefaultRequestHeaders.Add("Authorization", "Bearer "+token);
HttpResponseMessage httpresponse = await client.GetAsync(new Uri("https://graph.microsoft.com/v1.0/me/events/{id}/attachments/{id}"));
StorageFile downloadedfile = await ApplicationData.Current.LocalFolder.CreateFileAsync("attachment.txt",CreationCollisionOption.ReplaceExisting);
JObject resObj = JObject.Parse(await httpresponse.Content.ReadAsStringAsync());
string contentbyte = resObj["contentBytes"].ToString();
await FileIO.WriteTextAsync(downloadedfile, Encoding.UTF8.GetString(Convert.FromBase64String(contentbyte)));
更新:
如果附件不是.txt
,实际需要的是将base64编码的内容正确传输到文件,例如:
StorageFile downloadedfile = await ApplicationData.Current.LocalFolder.CreateFileAsync("attachment.xlsx", CreationCollisionOption.ReplaceExisting);
string contentbyte = "contentbyte";
byte[] filecontent = Convert.FromBase64String(contentbyte);
await FileIO.WriteBytesAsync(downloadedfile, filecontent);
我正在使用 Microsoft Graph
API 开发 UWP 应用程序。我正在使用以下 API
logged in
用户当天的会议列表
https://graph.microsoft.com/v1.0/me/calendarView?startDateTime=2017-06-20T20:00:00.0000000&endDateTime=2017-06-21T10:00:00.0000000
创建会议时,我在邀请函中附加了 document
给受邀参与者。
JSON
收到的响应有 "hasAttachments": true,
。我的要求是下载邀请中发送的文件。
我需要使用我的应用程序下载这些文件,然后附加它们并将其发送到 participants
。我该怎么做?
My requirement is to download the file that were send in the invite.
您似乎正在使用 List calendarView API to get the events. In that case you should be able get the event "id" property of the Event 您想要从响应中获得的资源,请检查 "id": "string (identifier)"
。
之后,您可以通过List attachments API, and get a special one attachment by Get attachment. You could get the binary contents of the attached file by contentBytes
property which contains the base64-encoded contents of the file. If your attachment is fileAttachment resource type. For example, if the attachment is a .txt
file, you can download it and save it in application local folder获取本次活动的所有附件,如下:
HttpClient client = new HttpClient();
string token =await AuthenticationHelper.GetTokenForUserAsync();
client.DefaultRequestHeaders.Add("Authorization", "Bearer "+token);
HttpResponseMessage httpresponse = await client.GetAsync(new Uri("https://graph.microsoft.com/v1.0/me/events/{id}/attachments/{id}"));
StorageFile downloadedfile = await ApplicationData.Current.LocalFolder.CreateFileAsync("attachment.txt",CreationCollisionOption.ReplaceExisting);
JObject resObj = JObject.Parse(await httpresponse.Content.ReadAsStringAsync());
string contentbyte = resObj["contentBytes"].ToString();
await FileIO.WriteTextAsync(downloadedfile, Encoding.UTF8.GetString(Convert.FromBase64String(contentbyte)));
更新:
如果附件不是.txt
,实际需要的是将base64编码的内容正确传输到文件,例如:
StorageFile downloadedfile = await ApplicationData.Current.LocalFolder.CreateFileAsync("attachment.xlsx", CreationCollisionOption.ReplaceExisting);
string contentbyte = "contentbyte";
byte[] filecontent = Convert.FromBase64String(contentbyte);
await FileIO.WriteBytesAsync(downloadedfile, filecontent);