Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

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

...

Required parameters

NameExampleDescription
uploadIdItemId1234id ItemId 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


Code Block
languagec#
/// <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 uploadIditemId)
{
    var url = "UploadFileChunk.js?accesskey=" + accessKey + "&uploadiditemid=" + uploadId 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}");
        }
    }
}

...