使用REST API将文件上传到Onedrive [英] Uploading files to Onedrive using REST API

查看:585
本文介绍了使用REST API将文件上传到Onedrive的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用其REST API将文件上传到OneDrive.这是根据 OneDrive Rest API :

I am trying to upload a file into OneDrive using its REST API. This is what I am trying to accomplish based on documentation available at OneDrive Rest API:

POST https://apis.live.net/v5.0/me/skydrive/files?access_token=ACCESS_TOKEN
Content-Type: multipart/form-data; boundary=A300x

--A300x
Content-Disposition: form-data; name="file"; filename="HelloWorld.txt"
Content-Type: application/octet-stream

Hello, World!
--A300x--

这就是我所拥有的:

Uri destination = new Uri(string.Format("https://apis.live.net/v5.0/{0}/files?", folder.ID));

BackgroundUploader uploader = new BackgroundUploader ();
uploader.SetRequestHeader("Authorization", "Bearer " + account.AccessToken);
uploader.SetRequestHeader("Content-Type", "multipart/form-data; boundary=\"foo_bar_baz\"");

List<BackgroundTransferContentPart> parts = new List<BackgroundTransferContentPart>();

BackgroundTransferContentPart metaDataPart = new BackgroundTransferContentPart();
metaDataPart.SetHeader("Content-Disposition", "form-data; name=\"file\";filename=\""+content.Name+"\"");
parts.Add(metaDataPart);

BackgroundTransferContentPart contentPart = new BackgroundTransferContentPart();
contentPart.SetHeader("Content-Type", content.ContentType);
// content is a StorageFile
contentPart.SetFile(content);

response.UploadOperation = await uploader.CreateUploadAsync(destination, parts, "form-data", "foo_bar_baz");

下面的这一行会导致访问冲突错误,并且Windows Store应用程序崩溃:

response.UploadOperation = await uploader.CreateUploadAsync(destination, parts, "form-data", "foo_bar_baz");

推荐答案

您正在创建两个BackgroundTransferContentPart,仅将拳头添加到列表"中.

You are creating two BackgroundTransferContentPart and only adding the fist to your 'List'.

我认为您只需要一个,就像这样:

I think you only need one, something like this:

List<BackgroundTransferContentPart> parts = new List<BackgroundTransferContentPart>();

BackgroundTransferContentPart metaDataPart = new BackgroundTransferContentPart();
metaDataPart.SetHeader("Content-Disposition",
    "form-data; name=\"file\";filename=\"" + content.Name + "\"");
metaDataPart.SetHeader("Content-Type", content.ContentType);
metaDataPart.SetFile(content);
parts.Add(metaDataPart);

更新:好的,以上代码解决了访问冲突问题.为什么获得400分错误是个谜.

UPDATE: Ok, the above code fixed the Access Violation issue. Why you are getting a 400 error is a mystery.

但是将文件上传到OneDrive的另一种方法是使用PUT方法:

But another way to upload a file to OneDrive is using the PUT method:

Uri putUri = new Uri(string.Format("https://apis.live.net/v5.0/{0}/files/{1}",
    "folder.a4fb14adbccd1917.A4FB14ADBCCD1917!32089",
    content.Name));

BackgroundUploader uploader = new BackgroundUploader();
uploader.SetRequestHeader("Authorization", "Bearer " + accessToken);
uploader.Method = "PUT";

UploadOperation putOperation = uploader.CreateUpload(putUri, content);
await putOperation.StartAsync();

您尝试过PUT吗?

这篇关于使用REST API将文件上传到Onedrive的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆