Uploading and Downloading Files to WebDAV Server
Uploading Files
The following 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);
}
}
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.
IT Hit WebDAV Server Engine for .NET also support upload of files of unlimited size. Usually to be able to handle files over 2Gb you have to create HttpListener-based server. WebDAV Servers hosted in IIS/ASP.NET-based will not be able to process files over 2Gb because of IIS and ASP.NET limitation. Read the WebDAV Server Engine documentation to find how to build a server that supports file uploads over 2 GB.
Upload Buffering
To prevent file content buffering on a client side set the IConnectionSettings.AllowWriteStreamBuffering property to false prior starting the upload:
IResource resource = folder.CreateResource("products.xlsx");
resource.AllowWriteStreamBuffering = false;
resource.TimeOut = 36000000; // 10 hours
resource.Upload("C:\\products.xlsx");
If you do not set this property the .NET Framework will buffer the entire file in memory prior to sending to server. This may significantly reduce file size that the API can upload.
Some servers however, such as some builds of SharePoint Services 3.0 require file content to be buffered on a client side. In this case set the AllowWriteStreamBuffering property to true. For best compatibility AllowWriteStreamBuffering is set to true by default and IConnectionSettings.SendChunked is set to false.
Usually prior to uploading large files you will also set the IConnectionSettings.TimeOut property specifying amount of milliseconds before timeout.
Downloading Files
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");
IResource resource = session.OpenResource(new Uri("http://server:8080/Products/image.gif"));
resource.TimeOut = 36000000; // 10 hours
resource.Download("C:\\image.gif");
To download a file and save it directly to the database or any other storage:
IResource resource = session.OpenResource("http://server:8080/Products/image.gif");
resource.TimeOut = 36000000; // 10 hours
using (Stream webStream = resource.GetReadStream())
{
int bufSize = 1048576; // 1Mb
byte[] buffer = new byte[bufSize];
int bytesRead = 0;
using (FileStream fileStream = File.OpenWrite(resource.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.
|