0
votes

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");
1
Have you try putting try/catch blocks around your code to see if any exception is been thrown?kiewic
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.Jack_2060
What about Fiddler or Network Monitor traces?kiewic
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.Jack_2060
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

1 Answers

1
votes

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?