0

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

This is what I have:

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

This line below causes an Access violation error and the Windows Store app crashes:

response.UploadOperation = await uploader.CreateUploadAsync(destination, parts, "form-data", "foo_bar_baz");
Palec
  • 12,743
  • 8
  • 69
  • 138
AGCodes
  • 622
  • 1
  • 8
  • 22
  • Have you try putting try/catch blocks around your code to see if any exception is been thrown? – kiewic Oct 02 '14 at 00:26
  • Exception comes out of a cpp file(background api) and is not caught in (Exception e) block. There is a try catch around this entire code. – AGCodes Oct 02 '14 at 00:51
  • What about Fiddler or Network Monitor traces? – kiewic Oct 02 '14 at 04:50
  • I do not think I even reach that stage as this will only create an UploadOperation. Then I will have to use it to start the actual upload. This is windows store app background transfer api call. – AGCodes Oct 02 '14 at 14:26
  • I have always used PUT to upload to SkyDrive/OneDrive. But if you want to use POST, make sure that your body is EXACTLY like they specify including all "-" characters, returns and line feeds. – Jon Oct 02 '14 at 17:21
  • Can you please post code for PUT, i can use PUT, but could not figure out the code for it. – AGCodes Oct 02 '14 at 17:54
  • Have you tried to modifying the example projects in https://github.com/liveservices/LiveSDK-for-Windows to get this scenario working iteratively? – Peter Nied Oct 02 '14 at 19:10
  • The scenarios for windows store app at that link uses LiveSDK with LiveConnectClient. I am using the REST API from LiveSDK, there is no matching example that i could find. – AGCodes Oct 02 '14 at 20:27
  • After re-reading your post, it looks like you are trying to pass the access_token as a header instead of adding it to the end of the url. – Jon Oct 02 '14 at 20:28

1 Answers1

1

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

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

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

Have you tried PUT?

Community
  • 1
  • 1
kiewic
  • 15,852
  • 13
  • 78
  • 101