2017-03-02 202 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

相关问题