1

I am using below Microsoft Graph API code to upload files to OneDrive for business of currently logged in user. The code uploads notepad .txt files fine and I can open file properly with content as it is. But when it uploads a .docx (word document), on opening attempt it throws error as file corrupted. What I am missing here? reference used - https://blog.mastykarz.nl/2-practical-tips-office-365-group-files-api/ https://graph.microsoft.io/en-us/docs/api-reference/v1.0/api/item_uploadcontent

Code:

byte[] filebytes= fileuploadControl.FileBytes;
using (var client = new HttpClient())
{
using (var request = new HttpRequestMessage(HttpMethod.Put, "https://graph.microsoft.com/v1.0/me/drive/root/children/" + filename + "/content"))
{
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
request.Headers.Add("Accept", "application/json;odata.metadata=verbose");
request.Content = new StringContent(DecodeFrom64(Convert.ToBase64String(filebytes)),System.Text.Encoding.ASCII, "text/plain");
using (HttpResponseMessage response = await client.SendAsync(request))
{
     if (response.IsSuccessStatusCode)
                        {
                            lblFileUpload.Text = "File uploaded successfully";
                        }
                    }
                }
            }
static public string DecodeFrom64(string encodedData)
    {
        byte[] encodedDataAsBytes = System.Convert.FromBase64String(encodedData);
        string returnValue = System.Text.ASCIIEncoding.ASCII.GetString(encodedDataAsBytes);
        return returnValue;
    }

2 Answers2

1

Below code worked for me perfectly. Code

var fileUrl = new Uri("https://graph.microsoft.com/v1.0/me/drive/root/children/" + filename + "/content");
                    var request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(fileUrl);
                    request.Method = "PUT";
                    request.ContentLength = filebytes.Length;
                    request.AllowWriteStreamBuffering = true;
                    //request.Accept = "application/json;odata=verbose";
                    request.ContentType = "text/plain";
                    request.Headers.Add("Authorization", "Bearer " + accessToken);
                    System.IO.Stream stream = request.GetRequestStream();
                    //filestream.CopyTo(stream);
                    stream.Write(filebytes, 0, filebytes.Length);
                    stream.Close();

                    System.Net.WebResponse response = request.GetResponse();
  • HI Ashish, I tried the above code to upload a word document but I'm still getting the error saying file is corrupted. Could you please let me know what value is present in the filebytes variable? Thanks. – user318197 Apr 18 '17 at 16:55
0

Given that you're in c# I don't think you need to base64 decode the contents first. Have you tried removing the decoding part and only encode it for the request?

  • appreciate your prompt reply. I tried below code and none of them working //request.Content = new StringContent((Convert.ToBase64String(filebytes)), System.Text.Encoding.ASCII, "text/plain"); //request.Content = new StreamContent(filestream, filesize); //request.Content = new ByteArrayContent(filebytes,0,filesize-1); Not sure where it is going wrong specifically with Office MIME types. – Ashish Trivedi Mar 25 '16 at 13:15
  • I am getting my file from file upload control. I am directly trying to use the file bytes or stream to push the file content. – Ashish Trivedi Mar 25 '16 at 13:26
  • I just tried using client, rathern httprequestmessage. client.PutAsync("https://graph.microsoft.com/v1.0/me/drive/root/children/" + filename + "/content", new StreamContent(filestream))) and bad request coming out of it. – Ashish Trivedi Mar 25 '16 at 14:08