0

I have been at this for a week and it's getting frustrating. On the final upload request, I get the error "Invalid Content-Range header." All other chunk uploads are successful, only the last one gets this error. How do I set the "Content-Range" header for uploading file chunks using the MS Graph API?

This is my latest code:

        using (var fileStream = File.OpenRead(attachmentLocation))
        {
            var buffer = new byte[1000 * 1024];
            int bytesRead;
            long totalBytesUploaded = 0;

            while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) > 0)
            {
                var requestEmail = new RestRequest(upload_url, Method.Put);

                requestEmail.AddHeader("Content-Type", "application/octet-stream");
                //requestEmail.AddHeader("Content-Length", bytesRead);                    
                requestEmail.AddHeader("Content-Range", $"bytes {totalBytesUploaded}-{totalBytesUploaded + bytesRead - 1}/{fileStream.Length}");

                requestEmail.AddBody(buffer, ContentType.Json);

                var response = client.Execute(requestEmail);
                totalBytesUploaded += bytesRead;
            }
        }

1 Answer 1

1

The last content range must take into account the file size and set the correct end of the range

Something like

if (totalBytesUploaded + bytesRead - 1 >= fileStream.Length)
{
    requestEmail.AddHeader("Content-Range", $"bytes {totalBytesUploaded}-{fileStream.Length - 1}/{fileStream.Length}");
}
else
{
    requestEmail.AddHeader("Content-Range", $"bytes {totalBytesUploaded}-{totalBytesUploaded + bytesRead - 1}/{fileStream.Length}");
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.