2014-10-02 119 views
0

我正嘗試使用其REST API將文件上傳到OneDrive。這就是我要完成的基礎上提供的文檔在OneDrive Rest API使用REST API將文件上傳到Onedrive

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應用商店應用程序崩潰:

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

您是否嘗試在代碼周圍放置try/catch塊以查看是否引發了任何異常? – kiewic 2014-10-02 00:26:03

+0

異常來自cpp文件(背景api),並未捕獲(異常e)塊。有一個嘗試繞過這整個代碼。 – 2014-10-02 00:51:19

+0

提琴手或網絡監視器跟蹤怎麼樣? – kiewic 2014-10-02 04:50:37

回答

1

您正在創建兩個BackgroundTransferContentPart,只將拳頭加入你的'列表'。

我想你只需要一個,像這樣:

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); 

UPDATE:好了,上面的代碼固定的訪問衝突問題。爲什麼你是getting a 400 error is a mystery

但另一種方式來上傳文件到OneDrive使用PUT方法:

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

+0

我試過這個,但是這給了BadRequest(400)錯誤。 – 2014-10-06 17:17:19

+0

我也試過PUT,它也給出了不好的要求。對你起作用嗎? – 2014-10-07 20:03:33

+0

是的,它適用於我。確保使用您自己的文件夾ID。 – kiewic 2014-10-07 21:42:12

相關問題