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

The IResumableUpload type exposes the following members.

Methods
  NameDescription
Public methodCode exampleCancelUpload
Cancels upload of the file.
Public methodCancelUpload(String)
Cancels upload of the file.
Public methodGetBytesUploaded
Amount of bytes successfully uploaded to server.
Public methodGetWriteStream(Int64, Int64, Int64)
Saves files's partial content to WebDAV server.
Public methodGetWriteStream(Int64, Int64, Int64, String)
Saves files's partial content to WebDAV server.
Public methodGetWriteStream(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 GetWriteStream(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 GetWriteStream(Int64, Int64, Int64) method.

To restore the upload call the GetBytesUploaded 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 GetWriteStream(Int64, Int64, Int64).

To completely break the upload call CancelUpload. 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 = session.OpenFile("http://server:8580/file1.txt");    
bool resumableUploadSupported =
              ((file.SupportedFeatures()).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 = session.OpenFolder(new Uri("http://server:8580/Sales"));
       FileInfo file = new FileInfo("C:\\LargeFile.exe");
       IFile file = folder.CreateFile(file.Name, lockInfo.LockToken.LockToken);
       resumableUpload = file.ResumableUpload;
       using (Stream webStream = file.GetWriteStream("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 = fileStream.Read(buffer, 0, bufSize))>0)
                    webStream.Write(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.
       resumableUpload.CancelUpload(); //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 = session.OpenFile("http://server:8580/"; + file.Name);
long bytesUploaded = file.ResumableUpload.GetBytesUploaded();
using (Stream webStream = file.IResumableUpload.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)
        {
            webStream.Write(buffer, 0, bytesRead);
        }
    }
}
See Also