DC 6.0 UploadFileChunks

This method is used for uploading the physical file to the server

URL : /UploadFileChunk.js

Method : POST

Auth required : Yes

Content type : application/octet-stream

Required parameters

NameExampleDescription
ItemId1234ItemId of the upload, returned from InitiateUpload
jsonresponse1decides whether the response should be json or xml
finished1/0bit that decides if the upload is complete
AccessKey
AccessKey for authentication


C# example

/// <summary>
/// Uploads the file in chunks. It happens more than often that consumers are uploading files that exceed the 2GB barrier of IIS.
/// Therefore we use chucked uploads. The chunk size may be set higher for more performance if the connection is rock solid.
/// </summary>
/// <param name="filePath">The file path.</param>
/// <param name="uploadId">The upload identifier.</param>
private void UploadFileChunks(string filePath, string itemId)
{
    var url = "UploadFileChunk.js?accesskey=" + accessKey + "&itemid=" + itemId+ "&jsonresponse=1&finished=";

    var stopWatch = new Stopwatch();

    const int ChunkSize = 1024 * 500;
    var bytes = new byte[ChunkSize];
    int read;
    var stream = File.OpenRead(filePath);
    while ((read = stream.Read(bytes, 0, ChunkSize)) > 0)
    {
        var finished = (read < ChunkSize) ? "1" : "0";
        var req = CreateWebRequest("POST", url + finished, "application/octet-stream");
        var resp = "";
        var code = HttpStatusCode.ExpectationFailed;
        try
        {
            stopWatch.Start();
            using (var streamWriter = new BinaryWriter(req.GetRequestStream()))
                streamWriter.Write(bytes, 0, read);

            using (var response = (HttpWebResponse)req.GetResponse())
            using (var responseStream = response.GetResponseStream())
            {
                code = response.StatusCode;
                if (responseStream != null)
                {
                    using (var reader = new StreamReader(responseStream))
                        resp = reader.ReadToEnd();
                }
            }
            stopWatch.Stop();
        }
        finally
        {
            log.Debug($"{nameof(UploadFileChunks)} {Convert.ToInt32(code)}: Request completed in {stopWatch.ElapsedMilliseconds} ms (accumulated), Response: {resp}, using {req.RequestUri}");
        }
    }
}