我的要求是下載在邀請中發送的文件。
看起來你似乎正在使用List calendarView API來獲取事件。在這種情況下,您應該能夠從響應中獲得Event資源的事件「id」屬性,請檢查"id": "string (identifier)"
。
之後,您可以通過List attachments API獲取此活動的所有附件,並通過Get attachment獲取特殊附件。您可以通過contentBytes
屬性獲取附加文件的二進制內容,該屬性包含文件的base64編碼內容。如果您的附件是fileAttachment resource type。例如,如果附件是一個.txt
文件,你可以下載它,並把它保存在應用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);
您嘗試了什麼,以及行爲與預期的不同?顯示您爲解決問題而付出的努力(和代碼)將有助於吸引回答。 –