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.)