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"));
IFile file = folder.CreateFile("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");
IFile file = folder.CreateFile(file.Name);
using (Stream webStream = file.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 a 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 the server use WebDavSession.GetAuthenticationScheme method:

string license = "<?xml version='1.0' encoding='u...
WebDavSession session = new WebDavSession(license);
session.Credentials = new NetworkCredential("User1", "pwd");
IFile file = session.OpenFile("http://server:8580/LargeFile.msi");
AuthenticationSchemes auth = session.GetAuthenticationScheme("http://server:8580/");
if (auth == AuthenticationSchemes.Ntlm)
    file.AllowWriteStreamBuffering = true; // Usually servers with Ntlm authentication does not allow unbuffered requests.
    file.AllowWriteStreamBuffering = false; // Basic, Digest, Kerberos or Anonymous can be unbuffered.

file.TimeOut = 36000000; // 10 hours

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 the first request only on the 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:

file.TimeOut = 36000000; // timeout in milliseconds 

Next Article:

Downloading Files from WebDAV Server Using .NET API, C#