admin管理员组文章数量:1401469
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
1 Answer
Reset to default 1The 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}");
}
版权声明:本文标题:c# - RestSharp returns "Invalid Content-Range header." on final file chunk with MS Graph API - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744241577a2596813.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论