Oct 14 2016 05:33 AM
I am getting HTTP/1.1 400 Bad request error while uploading files larger than 250 MB to a SPO document library via REST API. Mine is a dedicate O365 tenant and has a file upload limit of 10GB. When tried uploading files larger than 250 MB via drag and drop it works perfectly fine. but geeting error when try to do the same via REST API. Below is my code snippet.
Code:
byte[] binary = System.IO.File.ReadAllBytes(filePath);
string fname = System.IO.Path.GetFileName(filePath);
string result = string.Empty;
//Url to upload file
string resourceUrl = siteurl + "/_api/web/GetFolderByServerRelativeUrl('" + libraryName + "')/Files/add(url='" + fname + "',overwrite=true)";
HttpWebRequest wreq = HttpWebRequest.Create(resourceUrl) as HttpWebRequest;
wreq.UseDefaultCredentials = false;
wreq.Headers.Add("Authorization", "Bearer " + accessToken);
wreq.Method = "POST";
wreq.Timeout = 1000000; //timeout should be large in order to upload file which are of large size
wreq.Accept = "application/json; odata=verbose";
wreq.ContentLength = binary.Length;
wreq.Headers.Add("binaryStringRequestBody", "true");
//take the document and get it ready to upload
Stream postStream = wreq.GetRequestStream();
postStream.Write(binary, 0, binary.Length);
postStream.Close();
//do the upload
HttpWebResponse restResponse = (HttpWebResponse)wreq.GetResponse();
//assuming it all works then we’ll get a chunk of JSON back
//with a bunch of metadata about the file we just uploaded
postStream = restResponse.GetResponseStream();
StreamReader postReader = new StreamReader(postStream);
string results = postReader.ReadToEnd();