2017-03-02 183 views
1

當使用files = await graphClient.Me.Drive.Root.ItemWithPath(Path).Children.Request().GetAsync();如何將下載的文件保存到指定的路徑?

文件下載的得到保存在C:\Users\someone\AppData\Local\Packages\4a9bc9b3-6484-4443-9a65-f36f7519b1d5_9b10f1p8kdmhr\AC\INetCache\3HUY71XE

public static async Task<System.IO.Stream > GetCurrentUserFileAsync(string Path, string FileName) 
{ 
    IDriveItemChildrenCollectionPage files = null; 
    try 
    { 
     var graphClient = AuthenticationHelper.GetAuthenticatedClient(); 
     files = await graphClient.Me.Drive.Root.ItemWithPath(Path).Children.Request().GetAsync(); 
     foreach (DriveItem item in files) 
     { 

      Debug.WriteLine("Got file: " + item.Name); 
      if (item.Name == FileName) 
      { 
       var fileContent = await DownloadFileAsync(item.Id); 
       return fileContent ; 
      } 
     } 

     return null; 

    } 

    catch (ServiceException e) 
    { 
     Debug.WriteLine("We could not get user files: " + e.Error.Message); 
     return null; 
    } 


} 

// Downloads the content of an existing file. 
public static async Task<Stream> DownloadFileAsync(string fileId) 
    { 
     Stream fileContent = null; 

     try 
     { 
      var graphClient = AuthenticationHelper.GetAuthenticatedClient(); 
      var downloadedFile = await graphClient.Me.Drive.Items[fileId].Content.Request().GetAsync(); 
      fileContent = downloadedFile; 
      Debug.WriteLine("Downloaded file content for file: " + fileId); 


     } 

     catch (ServiceException e) 
     { 
      Debug.WriteLine("We could not download the file. The request returned this status code: " + e.Error.Message); 
      return null; 
     } 

     return fileContent; 
    } 
+1

DownloadFileAsync的定義是什麼? –

回答

1

完整的解決方案是使用使用ApplicationData.Current.LocalFolder.Path這個代碼,因爲我們不允許任何其他地方寫文件,那麼相對路徑的應用程序來完成:

public static async Task<bool> TryDownloadFileAtPathAsync() 
    { 
     var createdFileId = await UserSnippets.CreateFileAsync(Guid.NewGuid().ToString(), STORY_DATA_IDENTIFIER); 
     var fileContent = await UserSnippets.GetCurrentUserFileAsync("/Documents","Imp.zip") ; 
     using (var fileStream = await Task.Run(() => System.IO.File.Create(ApplicationData.Current.LocalFolder.Path + "\\Imp.zip"))) 
     { 
      fileContent.Seek(0, SeekOrigin.Begin); 
      fileContent.CopyTo(fileStream); 
     } 
     return fileContent != null; 
    } 

有關詳細信息,應用程序的相對路徑可以查找此頁:ApplicationData Class

釷anks @RasmusW

0

如果await DownloadFileAsync(item.Id)檢索所產生的數據流中的文件,然後它是由你的方法GetCurrentUserFileAsync的調用者寫的流內容某處。

可使用此代碼

var fileContent = await GetCurrentUserFileAsync(onedrivepath, onedrivefilename); 
using (var fileStream = File.Create("C:\\localpath\\localfilename")) 
{ 
    fileContent.Seek(0, SeekOrigin.Begin); 
    fileContent.CopyTo(fileStream); 
} 
+0

我的代碼保存文件是: '使用(var fileStream = await Task.Run(()=> System.IO.File.Create(「C:\\ Temp \\ Imp.zip」))) fileContent.Seek(0,SeekOrigin.Begin); fileContent.CopyTo(fileStream); }' 但是我收到一條錯誤消息,說acces被拒絕。 指定路徑下的Users.Download文件失敗。例外:訪問路徑'C:\ Temp'被拒絕。 我嘗試了不同的路徑,但總是出現相同的錯誤消息。 – CMNico

+0

聲音就像運行該進程的用戶需要對該文件夾的寫入權限。 – RasmusW

+0

其實我發現我只能寫文件到相對於應用程序的文件夾中。它與用戶的文件夾訪問權無關。 – CMNico