C# 如何从OneDrive下载文件
我想从一个驱动器下载公用文件夹中的文件,但它不下载文件。 以下是场景: 在公用文件夹中,我有另一个文件夹,其中包含多个文件,可以广泛访问。 出于测试目的,我共享了公用文件夹中的所有文件(我不知道这是否是正确的共享方式) 以下链接供我下载该文件:C# 如何从OneDrive下载文件,c#,windows-phone-8,onedrive,C#,Windows Phone 8,Onedrive,我想从一个驱动器下载公用文件夹中的文件,但它不下载文件。 以下是场景: 在公用文件夹中,我有另一个文件夹,其中包含多个文件,可以广泛访问。 出于测试目的,我共享了公用文件夹中的所有文件(我不知道这是否是正确的共享方式) 以下链接供我下载该文件: 从共享文件夹链接 从公用文件夹链接 直接链接- 我正在使用BackgroundTransferRequest使用以下代码下载文件: string filePathToDownload = string.Empty, fileName = "111.mp3
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,但仅下载6KB
我如何从上面的链接(其中任何一个)直接下载该文件
谢谢 基本上,你不能。这些链接是指向显示您共享的文件的web内容的链接。如果您的场景不介意要求用户登录到OneDrive,那么您可以使用Live SDK访问这些文件 要从Live SDK访问公用文件夹,您需要使用Live SDK获取公用文件夹的文件夹id,或者将复制的URL中的id转换为Live SDK使用的格式:
folder.<user-id>.<folder-resid>
这样你就可以打电话了
https://apis.live.net:443/v5.0/folder.DBBC281099F4FE69.DBBC281099F4FE69!646/files?access_token=<valid_token>
https://apis.live.net:443/v5.0/folder.DBBC281099F4FE69.DBBC281099F4FE69!646/文件?访问\u令牌=
并检索各个文件的ID,然后可以通过Live SDK下载这些文件,详细信息如下:尝试类似的方法
//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();
}
}
下面是使用
txt
文件作为示例。查看此示例:如果将url中的单词redir
替换为download
,则会得到原始文件而不是网页,即
对于那些仍在寻找答案的人。 查找文件路径的最简单方法是转到web上的一个驱动器,右键单击所需文件并选择“嵌入”。在右边,我们看到信息窗口,将文件集成到一个页面中。iframe中是文件的源。然后我们必须将单词embed替换为单词download,就这样。看看这些吗?
//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();
}
}
using Microsoft.Live;
using Microsoft.Live.Controls;