上载文件时Microsoft图形API OneDrive文件损坏

时间:2020-10-14 00:25:40

标签: azure file-upload microsoft-graph-api onedrive microsoft-graph-sdks

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

这是使用OneDrive api上传的代码。 以前上传文件时文件没有损坏,但如果是最近上传,则作为损坏的文件上传。 你知道为什么吗? 该URL是一个上传会话,并且文件是通过该上传会话上传的。 查看文件的哈希码,将更改并上传文件。 (哈希值已调制。)

0 个答案:

没有答案