Provides support partial uploads and resuming broken uploads.
For a list of all members of this type, see IResumableUpload Members.
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 resource.
To upload chunks of resource content use the GetWriteStream method. This method will attach Content-Range: bytes XXX-XXX/XXX header only if partial content is submitted.
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.
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.
Following example demonstrates how to determine if server supports resumable upload:
string license = "<?xml version='1.0' encoding='utf...
WebDavSession session = new WebDavSession(license);
IResource resource = session.OpenResource("http://server:8580/file1.txt");
bool resumableUploadSupported =
(resource.SupportedFeatures().Features & Features.ResumableUpload) != 0;
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");
IResource resource = folder.CreateResource(file.Name, lockInfo.LockToken.LockToken);
resumableUpload = resource.ResumableUpload;
using (Stream webStream = resource.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 resources.
}
}
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");
IResource resource = session.OpenResource("http://server:8580/"; + file.Name);
long bytesUploaded = resource.ResumableUpload.GetBytesUploaded();
using (Stream webStream = resource.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);
}
}
}
Namespace: ITHit.WebDAV.Client
Assembly: ITHit.WebDAV.Client (in ITHit.WebDAV.Client.dll)
IResumableUpload Members | ITHit.WebDAV.Client Namespace