c# - RestSharp returns "Invalid Content-Range header." on final file chunk with MS Graph API - Stack Overflow

I have been at this for a week and it's getting frustrating. On the final upload request, I get th

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;
            }
        }

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;
            }
        }
Share Improve this question edited Mar 24 at 22:07 Mark asked Mar 24 at 16:07 MarkMark 1,1091 gold badge9 silver badges15 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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}");
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744241577a4564729.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信