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

Using the API you can download either the entire file or request only a part of a file. To download the entire file and save it to local file system:

string license = "<?xml version='1.0' encoding='u...
WebDavSessionAsync session = new WebDavSessionAsync(license);
session.Credentials = new NetworkCredential("User1", "pwd");
IFileAsync file = await session.OpenFileAsync(new Uri("http://server:8080/Products/image.gif"));
file.TimeOut = 36000000; // 10 hours
await file.DownloadAsync("C:\\image.gif");

To download a file and save it directly to the database or any other storage:

IFileAsync file = await session.OpenFileAsync("http://server:8080/Products/image.gif");
file.TimeOut = 36000000; // 10 hours
using (Stream webStream = await file.GetReadStreamAsync())
    int bufSize = 1048576; // 1Mb
    byte[] buffer = new byte[bufSize];
    int bytesRead = 0;
    using (FileStream fileStream = File.OpenWrite(file.DisplayName))
        while ((bytesRead = webStream.Read(buffer, 0, bufSize)) > 0)
            fileStream.Write(buffer, 0, bytesRead);

The GetReadStream method provides overloaded instances to specify starting byte of the file to download and length. Using this method you can download a single file using several threads at a time or resume broken downloads.

Next Article:

Locking WebDAV Items