首页 文章

使用OneDrive SDK从OneDrive下载文件

提问于
浏览
0

我正在尝试使用OneDrive SDK从OneDrive下载文件 . 我有一个我创建的UWP应用程序 .

我已连接到我的OneDrive帐户,但不理解该怎么做 . 有很多答案,但似乎它们与新的OneDrive SDK无关 .

我想在C#中使用这个方法 .

StorageFile downloadedDBFile = await ApplicationData.Current.LocalFolder.CreateFileAsync("\\shared\\transfers\\" + App.dbName, CreationCollisionOption.ReplaceExisting);
Item item = await oneDriveClient.Drive.Root.ItemWithPath("Apps/BicycleApp/ALUWP.db").Request().GetAsync();

oneDriveClient连接正常 . 我甚至得到了“项目” . 正如您所看到的,它位于OneDrive的子目录中 .

我在子目录中创建了一个名为downloadedDBFile的本地文件,因此我可以将OneDrive文件的内容复制到 .

我该怎么办?

我已经使用这种方法将文件上传到OneDrive,没有任何问题 .

IStorageFolder sf = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFolderAsync("shared\\transfers");
var folder = ApplicationData.Current.LocalFolder;
var files = await folder.GetFilesAsync();

StorageFile dbFile = files.FirstOrDefault(x => x.Name == App.dbName);
await dbFile.CopyAsync(sf, App.dbName.ToString(), NameCollisionOption.ReplaceExisting);
StorageFile copiedFile = await StorageFile.GetFileFromPathAsync(Path.Combine(ApplicationData.Current.LocalFolder.Path, "shared\\transfers\\" + App.dbName));

var randomAccessStream = await copiedFile.OpenReadAsync();
Stream stream = randomAccessStream.AsStreamForRead();


var item = await oneDriveClient.Drive.Special.AppRoot.Request().GetAsync();

txtOutputText.Text = "Please wait.  Copying File";

using (stream){var uploadedItem = await oneDriveClient.Drive.Root.ItemWithPath("Apps/BicycleApp/ALUWP.db").Content.Request().PutAsync<Item>(stream);}

提前致谢

1 回答

  • 3

    您要获取的Item对象不是文件内容,它很可能是有关该文件的信息 . 相反,您需要使用Content属性将文件内容作为流获取,然后可以将其复制到文件中 . 代码如下所示:

    using (var downloadStream = await oneDriveClient.Drive.Root.ItemWithPath("Apps/BicycleApp/ALUWP.db").Content.Request().GetAsync())
    {
        using (var downloadMemoryStream = new MemoryStream())
        {
            await downloadStream.CopyToAsync(downloadMemoryStream);
            var fileBytes = downloadMemoryStream.ToArray();
            await FileIO.WriteBytesAsync(downloadedDBFile, fileBytes);
        }
    }
    

    注意调用OneDrive的.Content,它返回一个流而不是Item对象 .

相关问题