How to Pause Upload, Cancel Upload, Restore Broken Upload Using .NET API, C#

With IT Hit WebDAV Client API you can pause, cancel and restore broken uploads as well as upload files over 2Gb to WebDAV server based on IT Hit WebDAV Server Engine hosted in IIS.

Probing Resumable Upload Support

To detect if the item supports resumable upload feature probe the IResumableUpload bit in Features enumeration:

WebDavSessionAsync session = new WebDavSessionAsync(license);
IFileAsync file = await session.OpenFileAsync("http://server:8580/LargeFile.exe");
bool resumableUploadSupported = (file.SupportedFeaturesAsync().Result.Features & Features.ResumableUpload) != 0;

Starting Upload

To upload chunks of file content use the IResumableUpload.GetWriteStream method:

private  Stream uploadStream;
private  IResumableUploadAsync resumableUpload;

public  async Task uploadThreadAsync()
{
    string license = " <?xml version='1.0' encoding='utf...
    WebDavSessionAsync session = new WebDavSessionAsync(license);
    IFolderAsync folder = await session.OpenFolderAsync(new Uri("http://server:8580/Sales"));
    FileInfo fileInfo = new FileInfo("C:\\LargeFile.exe");
    IFileAsync file = await folder.CreateFileAsync(fileInfo.Name);
    resumableUpload = file.ResumableUpload;
    using (Stream webStream = await file.ResumableUpload.GetWriteStreamAsync(0, fileInfo.Length, fileInfo.Length))
    {
        uploadStream = webStream;
        int bufSize = 1048576; // 1Mb
        byte[] buffer = new byte[bufSize];
        int bytesRead = 0;

        using (FileStream fileStream = fileInfo.Open(FileMode.Open, FileAccess.Read, FileShare.Read))
        {
            while ((bytesRead = fileStream.Read(buffer, 0, bufSize)) > 0)
            {
                try
                {
                    webStream.Write(buffer, 0, bytesRead);
                }
                catch (StreamClosedException)
                {
                    // Upload was terminated (paused) by user from another thread.
                    return;
                }
            }
        }
    }
}

You can start the upload by calling IResumableUpload.GetWriteStream or IItemContent.GetWriteStream method. The IResumableUpload.GetWriteStream method will attach Content-Range: bytes XXX-XXX/XXX header but only if the partial content is submitted. If the entire file is submitted, as in the above example, the Content-Range header will not be attached.

If the user closes stream from another thread StreamClosedException will be thrown.

Pausing Upload

To pause the upload, you will simply break the connection. Call Stream.Close() on a stream object returned by IResumableUpload.GetWriteStream or by IItemContent.GetWriteStream method:

//This is UI thread in which user wants to pause upload.
public void uiThreadPause()
{
    if (uploadStream != null)
    {
        uploadStream.Close(); //Break connection.
    }
}

Restoring Broken Upload

To restore broken upload, you must first request how much bytes were successfully saved on the server side. Use IResumableUpload.GetBytesUploaded method for this purpose. The example below first requests number of bytes uploaded to the server and then starts the upload from the next byte:

WebDavSessionAsync session = new WebDavSessionAsync(license);
FileInfo fileInfo = new FileInfo("C:\\LargeFile.exe");
IFileAsync file = await  session.OpenFileAsync("http://server:8580/" + fileInfo.Name);
long bytesUploaded = await file.ResumableUpload.GetBytesUploadedAsync();
using (Stream webStream = await file.ResumableUpload.GetWriteStreamAsync(bytesUploaded, fileInfo.Length - bytesUploaded, fileInfo.Length))
{
    int bufSize = 1048576; // 1Mb
    byte[] buffer = new byte[bufSize];

    using (FileStream fileStream = fileInfo.Open(FileMode.Open, FileAccess.Read, FileShare.Read))
    {
        fileStream.Seek(bytesUploaded, SeekOrigin.Begin);
        int bytesRead;
        while ((bytesRead = fileStream.Read(buffer, 0, bufSize)) > 0)
        {
            try
            {
                webStream.Write(buffer, 0, bytesRead);
            }
            catch (StreamClosedException)
            {
                // Upload was terminated (paused) by user from another thread.
                return;
            }
        }
    }
}

Canceling Upload

The server may create a temporary file(s) on the server side when upload started. To completely break the upload and remove any temporary files on the server side call IResumableUpload.CancelUpload method. This will signal to the server that you do not plan to restore the upload. Note that prior to calling CancelUpload you mast first break the connection:

//This is UI thread in which user wants to cancel upload.
public void uiThreadCancel()
{
    if (uploadStream != null)
    {
        uploadStream.Close(); //Break connection.
        resumableUpload.CancelUpload(); //Cancel upload if we are not going to resume it later so server could clean the resources.
    }
}