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 = await session.GetFileAsync("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.WriteStreamAsync method:

private  Stream uploadStream;
private  IResumableUpload resumableUpload;

public  async Task uploadThreadAsync()
{
    string license = " <?xml version='1.0' encoding='utf...
    WebDavSession session = new WebDavSession(license);
    IFolder folder = await session.GetFolderAsync(new Uri("http://server:8580/Sales"));
    FileInfo fileInfo = new FileInfo("C:\\LargeFile.exe");
    IFile file = await folder.CreateFileAsync(fileInfo.Name);
    resumableUpload = file.ResumableUpload;

    try {
        await resumableUpload.WriteStreamAsync(async (outputStream) =>
        {
            int bufSize = 1048576; // 1Mb
            byte[] buffer = new byte[bufSize];
            int bytesRead = 0;

            using (var fileStream = fileInfo.OpenRead())
            {
                while ((bytesRead = await fileStream.ReadAsync(buffer, 0, bufSize)) > 0)
                    await outputStream.WriteAsync(buffer, 0, bytesRead);
            }
        }, 0, fileInfo.Length, fileInfo.Length);
    }
    catch (StreamClosedException)
    {
        // Upload was terminated (paused) by user from another thread.
    }
}

You can start the upload by calling IResumableUpload.WriteStreamAsync . The IResumableUpload.WriteStreamAsync 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.WriteStreamAsync or by IItemContent.WriteStreamAsync 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 fileInfo = new FileInfo("C:\\LargeFile.exe");
IFile file = await session.GetFileAsync("http://server:8580/" + fileInfo.Name);
long bytesUploaded = await file.ResumableUpload.GetBytesUploadedAsync();

await file.ResumableUpload.WriteStreamAsync(async (outputStream) =>
{
    int bufSize = 1048576; // 1Mb
    byte[] buffer = new byte[bufSize];

    using (var fileStream = fileInfo.OpenRead())
    {
        fileStream.Seek(bytesUploaded, SeekOrigin.Begin);
        int bytesRead;
        while ((bytesRead = await fileStream.ReadAsync(buffer, 0, bufSize)) > 0)
        {
            outputStream.Write(buffer, 0, bytesRead);
        }
    }
}, bytesUploaded, fileInfo.Length - bytesUploaded, fileInfo.Length);

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.
    }
}