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... WebDavSession session = new WebDavSession(license); session.Credentials = new NetworkCredential("User1", "pwd"); session.TimeOut = 36000000; // 10 hours IFile file = await session.GetFileAsync(new Uri("http://server:8080/Products/image.gif")); await file.DownloadAsync("C:\\image.gif");
To download a file and save it directly to the database or any other storage:
WebDavSession session = new WebDavSession(license); session.TimeOut = 36000000; // 10 hours IFile file = await session.GetFileAsync("http://server:8080/Products/image.gif"); 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 GetReadStreamAsync 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.