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:

WebDavSession session = new WebDavSession(license);
IFile file = session.OpenFile("http://server:8580/LargeFile.exe");
bool resumableUploadSupported = (file.SupportedFeatures().Features & Features.ResumableUpload) != 0;

Starting Upload

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

private Stream uploadStream;
private IResumableUpload resumableUpload;

public void uploadThread()
{
        string license = " <?xml version='1.0' encoding='utf...";
        WebDavSession session = new WebDavSession(license);   
        IFolder folder = session.OpenFolder(new Uri("http://server:8580/Sales"));
        FileInfo file = new FileInfo("C:\\LargeFile.exe");
        IFile file = folder.CreateFile(file.Name);
        resumableUpload = file.ResumableUpload;
        using (Stream webStream = file.ResumableUpload.GetWriteStream(0, file.Length, file.Length))
        {
            uploadStream = webStream;
            int bufSize = 1048576; // 1Mb
            byte[] buffer = new byte[bufSize];
            int bytesRead = 0;

            using (FileStream fileStream = file.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:

WebDavSession session = new WebDavSession(license);       
FileInfo file = new FileInfo("C:\\LargeFile.exe");
IFile file = session.OpenFile("http://server:8580/" + file.Name);   
long bytesUploaded = file.ResumableUpload.GetBytesUploaded();
using (Stream webStream = file.ResumableUpload.GetWriteStream(bytesUploaded, file.Length - bytesUploaded, file.Length))
{
    int bufSize = 1048576; // 1Mb
    byte[] buffer = new byte[bufSize];

    using (FileStream fileStream = file.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.
    }
}