Home
english
Home
.NET Server
Java Server
.NET Client
AJAX Client
AJAX Browser
Map Drive
Pricing
Contacts
info@ithit.com



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 item supports resumable upload feature probe the ResumableUpload bit in Features enumeration:

WebDavSession session = new WebDavSession(license);

IResource resource = session.OpenResource("http://server:8580/LargeFile.exe");

bool resumableUploadSupported = (resource.SupportedFeatures().Features & Features.ResumableUpload) != 0;

Starting Upload

To upload chunks of resource content use the IResumableUpload.GetWriteStream method:

private Stream uploadStream;

private IResumableUpload resumableUpload;

 

public void uploadThread()

{

        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);

        resumableUpload = resource.ResumableUpload;

        using (Stream webStream = resource.ResumableUpload.GetWriteStream(0, file.Length, 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)

                {

                    try

                    {

                        webStream.Write(buffer, 0, bytesRead);

                    }

                    catch (StreamClosedException)

                    {

                        // Upload was terminated (paused) by user from another thread.

                        return;

                    }       

                }

            }

        }

}

You can start the upload by calling IResumableUpload.GetWriteStream or IItemContent.GetWriteStream method. The IResumableUpload.GetWriteStream method will attach Content-Range: bytes XXX-XXX/XXX header but only if partial content is submitted. If entire file is submitted, as in the above example, the Content-Range header will not be attached.

If 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.GetWriteStream or by IItemContent.GetWriteStream 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 server side. Use IResumableUpload.GetBytesUploaded method for this purpose. The example below first requests number of bytes uploaded to server and then starts upload from the next byte:

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.ResumableUpload.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)

        {

            try

            {

                webStream.Write(buffer, 0, bytesRead);

            }

            catch (StreamClosedException)

            {

                // Upload was terminated (paused) by user from another thread.

                return;

            }

        }

    }

}

Canceling Upload

The server may create temporary file(s) on server side when upload started. To completely break the upload and remove any temporary files on server side call IResumableUpload.CancelUpload method. This will signal to 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.

    }

}


What WebDAV software would you like to have?

Selected Customers:
Country: Norway
DnB NOR Group
Country: Finland
Bank of Finland
Country: United Kingdom
Bechtle Direct
Country: Sweden
BT Industries
Country: USA
California Chamber of Commerce
Country: Denmark
Danfoss Group
Country: Denmark
DFDS
Country: USA
Fluke Networks
Country: USA
HNI Corporation
Country: USA
IHS Inc
Country: USA
LandAmerica Financial Group
Country: Canada
Laurentian University
Country: USA
Microsoft
Country: Israel
RADVISION
Country: Ukraine
Raiffeisen Bank
Country: Netherlands
Sanoma Uitgevers
Country: USA
Siemens
Country: Australia
WorkCover NSW
Country: Ukraine
OTP Bank
Country: USA
Intel Corporation
Country: Austria
Austrian Federal Railways
Home .NET Server Java Server .NET Client AJAX Client AJAX Browser Map Drive Pricing Contacts

Updated: Sunday, October 26, 2008