Microsoft graph api onedrive file gets corrupted when uploading file RRS feed

  • Question

  • User-1404740798 posted
    public JObject UploadByteFile(string upn, string url, IFormFile files)
        int start = 0;
        var fileSize = Convert.ToInt32(files.Length);
        int fragSize = 4 * 1024 * 1024;   //1024 * 1024 * 4;
        byte[] file = new byte[fileSize];
        var arrayBatches = ByteArrayIntoBatches(file, fragSize);
        var responseCode = HttpStatusCode.OK;
        var jObject = new JObject();
        foreach (var byteArray in arrayBatches)
            int byteArrayLength = byteArray.Length;
            var contentRange = " bytes " + start + "-" + (start + (byteArrayLength - 1)) + "/" + file.Length;
            using (var client = new HttpClient())
                var content = new ByteArrayContent(byteArray);
                content.Headers.Add("Content-Length", byteArrayLength.ToString());
                content.Headers.Add("Content-Range", contentRange);
                var response = client.PutAsync(url, content);
                var strData = response.Result.Content.ReadAsStringAsync().Result;
                responseCode = response.Result.StatusCode;
                if(responseCode == HttpStatusCode.Created)
                    JObject data = JObject.Parse(strData);
                    string downloadUrl = data["@content.downloadUrl"].ToString();
                    string itemId = data["id"].ToString();
                    fileSize = fileSize / 1000;
                    jObject = JObject.FromObject(new { name = files.Name, id = itemId, url = downloadUrl, size = (double)fileSize });
                else if (responseCode == HttpStatusCode.Conflict)
                    var restart = RestartByteFile(upn, url, files.Name);
                    responseCode = restart;
            start = start + byteArrayLength;
        if (responseCode == HttpStatusCode.Created) return jObject;
        else return jObject = JObject.FromObject(new { result = "fail"});

    .net core API Code

    This is the code to upload using the OneDrive api.

    The file was not damaged when the file was previously uploaded, but if it was recently uploaded, it is uploaded as a damaged file.

    Can you see why?

    The URL is an upload session, and files are uploaded through the upload session.

    Looking at the hash code of the file, the file is altered and uploaded.

    (The hash value is modulated.)

    Wednesday, October 14, 2020 5:45 AM

All replies

  • User753101303 posted


    This is based on https://docs.microsoft.com/en-us/graph/api/driveitem-createuploadsession?view=graph-rest-1.0?

    When not seeing quickly somethign obvious I prefer to give a closer look at what happens before reading the code again. For example here I would likely use test data with a know pattern (for example byte 0 to 255 and repeat or a single byte value for each chunk) so that I can easily understand how the final file differs. I like also to always have a fallback for example here if I don't get one the two reponse code you seems to expect.

    Edit: never tried this particular area but I often favor using https://www.nuget.org/packages/Microsoft.Graph over crafting my own queries (not sure if and how this particular API is exposed).

    Thursday, October 15, 2020 9:44 AM
  • User-1404740798 posted

    I constructed the code by referring to the tutorial you gave me

    When comparing the original file and the file uploaded to One Drive through api, the uploaded file was damaged.

    The hash values of the original file and damaged file did not match.

    (File information was checked through PowerShell)

    Can I check out the upload tutorial for OneDrive SDK? 

    We would appreciate it if you let us know a site that you can refer to.

    thank you

    Thursday, October 15, 2020 10:58 AM