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



Uploading Files to WebDAV Server Using .NET API, C#

Uploading Files and Data

The following C# example uploads a file stored in file system:

 

string license = "<?xml version='1.0' encoding='u...

WebDavSession session = new WebDavSession(license);

session.Credentials = new NetworkCredential("User1", "pwd");

IFolder folder = session.OpenFolder(new Uri("http://server:8080/Sales"));

IResource resource = folder.CreateResource("products.xlsx");

resource.Upload("C:\\products.xlsx");

 

You can also upload a file or any data stored in a database or any other repository:

 

IFolder folder = session.OpenFolder(new Uri("http://server:8080/Sales"));

FileInfo file = new FileInfo("C:\\Products.exe");

IResource resource = folder.CreateResource(file.Name);

using (Stream webStream = resource.GetWriteStream(file.Length))

{

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

    }

}

Uploading Large Files

IT Hit WebDAV Client API for .NET supports file upload of any size (up to 8,589,934,592 Gb). However some servers like built-in Microsoft IIS WebDAV does not support file upload over 2 GB.

To be able to upload files files over 2Gb to WebDAV server based on IT Hit WebDAV Server Engine hosted in IIS you must implement resumable upload interfaces on server side and upload file using segments below 2Gb using IResumableUpload interface (see How to Pause Upload, Cancel Upload, Restore Broken Upload). You can also implement HttpListener-based server that does not have any upload limitations. Read the WebDAV Server Engine documentation to find how to build a server that supports file uploads over 2 GB.

Upload buffering

The WebDAV Client API provides IConnectionSettings.AllowWriteStreamBuffering property for managing content buffering on a client side. To avoid in-memory buffering when uploading files set this property to false. Servers with Basic, Digest, Kerberos and Anonymous authentication support unbuffered uploads while NTLM authentication by default - not. To determine the authentication method used by server use WebDavSession.GetAuthenticationScheme method:

 

string license = "<?xml version='1.0' encoding='u...

WebDavSession session = new WebDavSession(license);

session.Credentials = new NetworkCredential("User1", "pwd");

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

 

AuthenticationSchemes auth = session.GetAuthenticationScheme("http://server:8580/");

if (auth == AuthenticationSchemes.Ntlm)

    resource.AllowWriteStreamBuffering = true; // Usually servers with Ntlm authentication does not allow unbuffered requests.

else

    resource.AllowWriteStreamBuffering = false; // Basic, Digest, Kerberos or Anonymous can be unbuffered.

 

resource.TimeOut = 36000000; // 10 hours

resource.Upload("C:\\LargeFile.msi");

 

If AllowWriteStreamBuffering is set to true, the file content is buffered in memory, and the maximum file upload size will be limited to approximately 1/2 of physical memory size.

 

To allow unbuffered uploads for NTLM you will have to tune your server settings. You will have to set authentication for first request only on server side, otherwise you will get a "This request requires buffering data to succeed" exception.

For best compatibility AllowWriteStreamBuffering is set to true by default.


Upload Timeout

As soon as upload may take significant time usually you will increase the IConnectionSettings.TimeOut value:

resource.TimeOut = 36000000; // timeout in milliseconds


Selected Customers:
Country: Norway
DnB NOR Group
Country: Finland
Bank of Finland
USA
Symantec
Country: Sweden
Toyota
Country: Denmark
Danfoss Group
Country: USA
Microsoft
Country: Ukraine
Raiffeisen Bank
Country: USA
Siemens
Country: Ukraine
OTP Bank
Country: USA
Intel Corporation
Country: Austria
Austrian Federal Railways
Country: Israel
Autodesk, Inc.
Country: USA
U.S. Customs and Border Protection Agency
Home .NET Server Java Server .NET Client AJAX Client AJAX Browser Map Drive Pricing Contacts

Updated: Monday, May 10, 2010