2017-06-20 9 views
-2

我正在使用Microsoft Graph API開發UWP應用程序。我使用的是下面API使用UWP中的Microsoft Graph API獲取附加文檔的列表並將它們下載到本地存儲中

https://graph.microsoft.com/v1.0/me/calendarView?startDateTime=2017-06-20T20:00:00.0000000&endDateTime=2017-06-21T10:00:00.0000000 

在創建會議獲得當天的會議列表爲logged in用戶,我已經連接了在邀請被邀參加一個document。 收到的回覆有。我的要求是下載邀請中發送的文件。 我需要使用我的應用程序下載這些文件,然後附加它們並將其發送到participants。我怎樣才能做到這一點?

+2

您嘗試了什麼,以及行爲與預期的不同?顯示您爲解決問題而付出的努力(和代碼)將有助於吸引回答。 –

回答

0

我的要求是下載在邀請中發送的文件。

看起來你似乎正在使用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); 
+0

此代碼適用於文本文檔,其他文​​檔如.docx,.xls,.pdf和.pptx以及其他文檔如何? – Apoorv

+0

@Apoorv,你提到的這些文件格式應該可以工作。我測試了'.docx'和'.xls'它們運行良好。查看我的更新。 –

+0

還有一件事,我想檢查當用戶登錄時(系統時間)和當天結束時那裏的會議。我希望將時間轉換爲時區並相應顯示時間。我該怎麼做 ? – Apoorv

相關問題