Click or drag to resize

IResumableUploadAsync 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: 2.0.420.0
Syntax
public interface IResumableUploadAsync

The IResumableUploadAsync type exposes the following members.

Methods
  NameDescription
Public methodCode exampleCancelUploadAsync
Cancels upload of the file.
Public methodCancelUploadAsync(String)
Cancels upload of the file.
Public methodGetBytesUploadedAsync
Amount of bytes successfully uploaded to server.
Public methodGetWriteStreamAsync(Int64, Int64, Int64)
Saves files's partial content to WebDAV server.
Public methodGetWriteStreamAsync(Int64, Int64, Int64, String)
Saves files's partial content to WebDAV server.
Public methodGetWriteStreamAsync(Int64, Int64, Int64, String, String)
Saves files's partial content to WebDAV server.
Top
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 GetWriteStreamAsync(Int64, Int64, Int64) 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 returned by GetWriteStreamAsync(Int64, Int64, Int64) 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 calling GetWriteStreamAsync(Int64, Int64, Int64).

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...
WebDavSessionAsync session = new WebDavSessionAsync(license);            
IFileAsync file = await session.OpenFileAsync("http://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;
IResumableUploadAsync resumableUpload;

//This method performs uploading in separate thread.
public void uploadThread()
{
    try
    {
       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 file = new FileInfo("C:\\LargeFile.exe");
       IFileAsync file = await folder.CreateFileAsync(file.Name, lockInfo.LockToken.LockToken);
       resumableUpload = file.ResumableUpload;
       using (Stream webStream = await file.GetWriteStreamAsync("application/octet-stream", 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 = await fileStream.ReadAsync(buffer, 0, bufSize))>0)
                    await webStream.WriteAsync(buffer, 0, bytesRead);
            }
        }
    }
    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...
WebDavSessionAsync session = new WebDavSessionAsync(license);        
FileInfo file = new FileInfo("C:\\LargeFile.exe");
IFileAsync file = await session.OpenFileAsync("http://server:8580/"; + file.Name);
long bytesUploaded = await file.ResumableUpload.GetBytesUploadedAsync();
using (Stream webStream = await file.IResumableUploadAsync.GetWriteStreamAsync(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 = await fileStream.ReadAsync(buffer, 0, bufSize)) > 0)
        {
            webStream.Write(buffer, 0, bytesRead);
        }
    }
}
See Also