首页 文章

如何从OneDrive下载文件

提问于
浏览
4

我希望从One Drive下载公共文件夹中的文件,但它不下载文件 . 这是场景:

在公共文件夹中,我有另一个文件夹,其中包含多个文件,可以广泛访问 . 出于测试目的,我已经共享了公共文件夹中的所有文件(如果它是正确的共享方式,我不会这样做) .

提供以下链接供我下载文件:

我使用 BackgroundTransferRequest 使用以下代码下载文件:

string filePathToDownload = string.Empty, fileName = "111.mp3";
filePathToDownload = "http://1drv.ms/1z9XlW6";

Uri transferUri = new Uri(Uri.EscapeUriString(filePathToDownload), UriKind.RelativeOrAbsolute);
BackgroundTransferRequest transferRequest = new BackgroundTransferRequest(transferUri);
transferRequest.Method = "GET";
transferRequest.TransferPreferences = TransferPreferences.AllowCellularAndBattery;

Uri downloadUri = new Uri(DataSource.TEMPDOWNLOADLOCATION + fileName, UriKind.RelativeOrAbsolute);
transferRequest.DownloadLocation = downloadUri;
transferRequest.Tag = fileName;

该文件为300Kb,但这仅下载6 Kb .

如何从上面的链接(其中任何一个)直接下载文件?

谢谢!

4 回答

  • 0

    如果您在网址中将 redir 替换为 download ,则会获得原始文件而不是网页,即

    https://onedrive.live.com/download?resid=DBBC281099F4FE69%21646

  • 10

    基本上,你不能 . 这些链接是指向显示您共享文件的Web内容的链接 . 如果您的方案不介意要求用户登录OneDrive,则可以使用Live SDK访问这些文件 .

    要从Live SDK访问公用文件夹,您需要使用Live SDK获取公用文件夹的文件夹ID,或者将您复制的URL中的ID转换为Live SDK使用的格式:

    folder.<user-id>.<folder-resid>
    

    !之前的部分在哪里!通常,您不应构造ID,因为将来ID可能会更改,而您应该从服务中检索ID . 但是,使用您粘贴的URL,ID将为:

    folder.DBBC281099F4FE69.DBBC281099F4FE69!646
    

    这将允许你打电话

    https://apis.live.net:443/v5.0/folder.DBBC281099F4FE69.DBBC281099F4FE69!646/files?access_token=<valid_token>
    

    并检索单个文件的ID,然后您可以通过Live SDK下载这些详细信息:http://msdn.microsoft.com/en-US/library/dn659726.aspx#download_a_file

  • 0

    尝试这样的事情

    //we first need the file id
      string id = string.Empty;
      //we need to get all of the filenames stored in the root of the skydrive account
      LiveOperationResult result = await this.client.GetAsync("me/skydrive/files");
    
      //lets make a list of all these filenames
      List<object> items = result.Result["data"] as List<object>;
    
      //for every filename, check if it is what we want, in this case "sample.txt"
      //if it is what we want, get the id and save it to out already defined id value
      foreach (object item in items)
      {
          IDictionary<string, object> file = item as IDictionary<string, object>;
          if (file["name"].ToString() == "sample.txt")
          {
                id = file["id"].ToString();
          }
      }
    
      //to download the file we need to use the id + "/content"
      LiveDownloadOperationResult result2 = await client.DownloadAsync(string.Format("{0}/content", id));
    
      //once the file had downloaded, lets copy it to IsolatedStorage
     Stream stream = result2.Stream;
     using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
     {
         using (IsolatedStorageFileStream fileToSave = storage.OpenFile("sample.txt", FileMode.Create, FileAccess.ReadWrite))
         {
                stream.CopyTo(fileToSave);
                stream.Flush();
                stream.Close();
         }
    }
    

    这里 clientLiveConnectClient 类的对象 . 进口

    using Microsoft.Live;
    using Microsoft.Live.Controls;
    

    这里以 txt 文件为例 . 通过这个例子:http://www.baileystein.com/2013/10/20/skydrive-how-to-upload-and-download-a-text-file-on-wp8/

  • 5

    对于仍在寻找对该问题的回答的人 . 查找文件路径的最简单方法是转到Web上的One Drive并右键单击我们想要的文件,然后选择Embed . 在右侧,我们看到信息窗口将我们的文件集成到页面中 . iframe内部是文件的来源 . 然后我们必须用单词download替换嵌入这个词,就是这样 .

相关问题