SkyDrive REST API
-
Friday, April 27, 2012 3:02 PM
hi,
I want to upload a "large file" (5 to 10Mb).I want to do it by using multiple webrequest. All i got is http 400.
Here is my code :
string folderId = "folder....!112"; var path = @"D:\...\file.pdf"; var uri = string.Format("{0}{1}/files?{2}", SkyDrive.Utilities.ApiServiceUri, folderId, SkyDrive.Utilities.AccessToken); int count = 0; int offset = 0; int bufferSize = 170000; byte[] buffer = new byte[bufferSize]; using (var reader = new System.IO.FileStream(path, System.IO.FileMode.Open)) { while (offset < reader.Length) { offset += reader.Read(buffer, 0, bufferSize); count += bufferSize + 1; var request = (HttpWebRequest)WebRequest.Create(uri); request.Method = "POST"; request.Headers.Add("bits-packet-type", "Fragment"); request.Headers.Add("Content-Disposition", "file; filename=\"filename.pdf\""); request.ContentType = "multipart/form-data; boundary=A300x";// "application/octet-stream"; using (var stream = request.GetRequestStream()) { using (var writer = new System.IO.BinaryWriter(stream)) { try { writer.Write("--A300x\r\n"); writer.Write(@"Content-Disposition: form-data; name=""file""; filename=""filename.pdf""\r\n"); writer.Write(@"Content-Type: application/octet-stream\r\n"); writer.Write(buffer); writer.Write("\r\n--A300x--"); } catch (System.IO.EndOfStreamException excep) { throw new ApplicationException(excep.Message); } } } using (var stream = request.GetResponse().GetResponseStream()) { using (var responseReader = new System.IO.StreamReader(stream)) { var response = responseReader.ReadToEnd(); Console.WriteLine(response); } } } }Any suggestions?
Thanks
BriceR
All Replies
-
Friday, April 27, 2012 8:42 PMThere should have been an error message in the body of the HTTP 400 request. What did it say?
-
Monday, April 30, 2012 7:52 AM
Hi,
Sorry for the delay,
here is the response
{ "error": { "code": "request_body_invalid", "message": "The provided request entity body is not valid." } }
thanks
-
Monday, April 30, 2012 8:21 PM
Your post request is malformed. This code
writer.Write(@"Content-Type: application/octet-stream\r\n");
Should actually be
writer.Write(@"Content-Type: application/octet-stream\r\n\r\n");
I was wondering why you are using HTTP POST to upload files given that is more complicated and error prone than using HTTP PUT?
- Proposed As Answer by Dare Obasanjo - MSFT Monday, April 30, 2012 8:21 PM
- Marked As Answer by JOshiroModerator Wednesday, May 02, 2012 11:36 PM
- Unmarked As Answer by bibirodger Thursday, May 03, 2012 7:33 AM
-
Wednesday, May 02, 2012 4:01 PM
hi,
I use a http post because when you try to upload a file on skydrive from windows live website, they use http post. For the record, i try to use http put, but it ended the same way.
- Edited by bibirodger Thursday, May 03, 2012 7:34 AM
-
Wednesday, June 13, 2012 11:44 AM
hi,
may I know if the code is working for you now?
I'm getting error at using (var stream = request.GetResponse().GetResponseStream())
http 400 bad request error. i've tried with the /r/n and without it.. both same results.

