.NET Server

Resumable Upload

In this article

Resumable Upload

Resumable upload interfaces provide a reliable mechanism of uploading large files to WebDAV server and managing long-lasting uploads. After implementing resumable upload feature, you will be able to pause \ resume uploads, restore broken uploads, report upload progress and create upload progress bars.

To handle upload in your server implementation, you must implement IResumableUpload and IUploadProgress interfaces on resource items. IResumableUpload provides its own SaveFromStream method that will be always called instead of IResource.SaveFromStream. Here is the example of IResumableUpload.SaveFromStream method implementation:

public WebDAVResponse SaveFromStream(long startIndex, long totalContentLength, Stream segment, string contentType)
{
    long segmentSize = 0;
    try
    {
        segmentSize = SaveContent(segment, contentType, totalContentLength, startIndex); // save segment and totalContentLength
    }
    catch (HttpListenerException)
    {
        // connection broken or closed by client
    }
 
    if (startIndex + segmentSize < totalContentLength)
    {
        return new ResumeIncompleteResponse(); // more segments expected
    }
    else if (segmentSize > 0)
    {
        return new OkResponse(); // all segments received
    }
    else
    {
        return new NoContentResponse(); // content truncated to zero
    }
}

In this method, you will save segments of the file content in your persistent storage. The engine passes 2 additional parameters to this method - index in the file to which corresponds first byte in the content parameter and the total size of the resource being uploaded. You will also save totalContentLength in your persistent storage. You will later return value of this parameter from IResumableUpload.TotalContentLength property implementation.

From your IResumableUpload.SaveFromStream implementation you will return ResumeIncompleteResponse in the case when more segments are expected to arrive.

Note that if you implement IResumableUpload, IFolder.CreateResource method will always receive null stream and content will be always passed to IResumableUpload.SaveFromStream method.

From your IUploadProgress.UploadProgress implementation on resource items, you will return an array with only 1 item - this resource:

IResumableUpload[] IUploadProgress.UploadProgress
{
    get
    {
        IResumableUpload[] items = new IResumableUpload[1];
        items[0] = this;
        return items;
    }
}

For each item from the array, the engine will request Path, LastChunkSaved, BytesUploaded, TotalContentLength and submit this info to the client application.

Optionally you can implement IUploadProgress interface on folder items. In this case, you will return an array of items that are being uploaded to the server. Here is the typical class diagram, in this case:

The class diagram for IFolder and IResource

Next Article:

AJAX Upload Progress Bar