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}");
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744241577a4564729.html
评论列表(0条)