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 = await session.GetFolderAsync(new Uri("http://server:8080/Sales"));
IFile file = await folder.CreateFileAsync("products.xlsx");
await file.UploadAsync("D:\\products.xlsx");

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

FileInfo fileInfo = new FileInfo("C:\\Products.exe");
IFolder folder = await session.GetFolderAsync(new Uri("http://server:8080/Sales"));
IFile file = await folder.CreateFileAsync(fileInfo.Name);

await session.UploadAsync(file.Href, async (outputStream) =>
{
    int bufSize = 1048576; // 1Mb
    byte[] buffer = new byte[bufSize];
    int bytesRead = 0;

    using (var fileStream = fileInfo.OpenRead())
    {
        while ((bytesRead = await fileStream.ReadAsync(buffer, 0, bufSize)) > 0)
            await outputStream.WriteAsync(buffer, 0, bytesRead);
    }
}, null, fileInfo.Length, 0, -1, null, null);

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 uses content buffering offered by HttpClient on a client side. 

string license = "<?xml version='1.0' encoding='u...
WebDavSession session = new WebDavSession(license);
session.TimeOut = 36000000; // 10 hours
session.Credentials = new NetworkCredential("User1", "pwd");
IFileAsync file = await session.OpenFileAsync("http://server:8580/LargeFile.msi");

await file.UploadAsync("C:\\LargeFile.msi");

Upload Timeout

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

session.TimeOut = 36000000; // timeout in milliseconds 

Next Article:

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