Click or drag to resize

IResumableUpload Interface

IT Hit WebDAV Classes Reference
Provides support partial uploads and resuming broken uploads.

Namespace:  ITHit.WebDAV.Client
Assembly:  ITHit.WebDAV.Client (in ITHit.WebDAV.Client.dll) Version: 6.0.4052-Beta
Syntax
public interface IResumableUpload

The IResumableUpload type exposes the following members.

Methods
Remarks

Using this interface you can upload files to servers that support resumable upload, pause, cancel and restore broken uploads. You can also request amount of bytes successfully saved on server side.

To detect if server supports resumable upload feature probe the ResumableUpload bit in Features enumeration. The server will return resumable-upload token in DAV header in response to OPTIONS request if resumable upload is supported by the file.

To upload chunks of the file content use the [!:WriteStreamAsync(Func,long,long,long,string,LockUriTokenPair[])] method. This method will attach Content-Range: bytes XXX-XXX/XXX header only if partial content is submitted.

To pause the upload you will simply break the connection. Call Stream.Close() on a stream object passed to [!:WriteStreamAsync(Func,long,long,long,string,LockUriTokenPair[])] method.

To restore the upload call the GetBytesUploadedAsync method. It will return number of bytes successfully uploaded and saved on server side. Use this value to resume the upload from the next byte using stream passed to [!:WriteStreamAsync(Func,long,long,long,string,LockUriTokenPair[])].

To completely break the upload call CancelUploadAsync. This will signal to server that you do not plan to restore the upload and all temporary files on server side, if any, can be deleted.

Examples

Following example demonstrates how to determine if server supports resumable upload:

string license = "<?xml version='1.0' encoding='utf...
WebDavSession session = new WebDavSession(license);            
IFile file = await session.GetFileAsync("https://server:8580/file1.txt");    
bool resumableUploadSupported =
              ((await file.SupportedFeaturesAsync()).Features & Features.ResumableUpload) != 0;

Examples

The following example demonstrates pausing and canceling the upload. One stream performs upload while it is paused from another stream by closing connection.

private Stream uploadStream;
IResumableUpload resumableUpload;

//This method performs uploading in separate thread.
public void uploadThread()
{
    try
    {
        string license = "<?xml version='1.0' encoding='utf...
        WebDavSession session = new WebDavSession(license);
        IFolder folder = await session.GetFolderAsync(new Uri("https://server:8580/Sales"));
        FileInfo file = new FileInfo("C:\\LargeFile.exe");
        LockInfo lockInfo = await folder.LockAsync(LockScope.Exclusive, true, "User 1", new TimeSpan(0, 30, 0));
        Uri RemoteStorageUri = new Uri($"https://server:8080/Sales/{file.Name}");
        LockUriTokenPair[] lockTokens = new LockUriTokenPair[] { new LockUriTokenPair(RemoteStorageUri, lockInfo.LockToken.LockToken) };
        IFile davFile = await folder.CreateFileAsync(file.Name, lockTokens);
        IResumableUpload resumableUpload = davFile.ResumableUpload;

        Stream uploadStream = null;
        await session.UploadAsync(RemoteStorageUri, async (outputStream) =>
        {
            uploadStream = webStream;

            int bufSize = 1048576; // 1Mb
            byte[] buffer = new byte[bufSize];
            int bytesRead = 0;

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

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

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

Examples

The following example demonstrates how to restore (resume) broken upload. This example first requests number of bytes uploaded to server and then starts upload from the next byte.

string license = "<?xml version='1.0' encoding='utf...
WebDavSession session = new WebDavSession(license);        
FileInfo file = new FileInfo("C:\\LargeFile.exe");
IFile file = await session.GetFileAsync("https://server:8580/"; + file.Name);
long bytesUploaded = await file.ResumableUpload.GetBytesUploadedAsync();
await davFile.ResumableUpload.WriteStreamAsync(async (outputStream) =>
        {
            int bufSize = 1048576; // 1Mb
            byte[] buffer = new byte[bufSize];

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