Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

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
uploadId1234id of the upload
jsonresponse1decides whether the response should be json or xml
finished1/0bit that decides if the upload is complete


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 uploadId)
{
    var url = "UploadFileChunk.js?accesskey=" + accessKey + "&uploadid=" + uploadId + "&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}");
        }
    }
}
  • No labels