Namespace: ITHit.WebDAV.Server
Exception | Condition |
---|---|
LockedException | The file was locked and client did not provide lock token. |
NeedPrivilegesException | The user doesn't have enough privileges. |
InsufficientStorageException | Quota limit is reached. |
DavException | In other cases. |
IIS and ASP.NET does not support files upload larger than 2Gb. If you need to upload files larger than 2Gb you must develop HttpListener-based WebDAV server or implement resumable upload interfaces.
If you are creating HttpHandler-based WebDAV server you must specify the file maximum upload size in web.config of your web application. By default maximum upload size is set to 4096 KB (4 MB) by ASP.NET. This limit is used to prevent denial of service attacks caused by users posting large files to the server. To increase the upload limit add <httpRuntime> section to your web application web.config file and specify the limit in kilobytes:
<configuration> <system.web> ... <httpRuntime maxRequestLength="2097151" /> //2Gb ... </system.web> </configuration>
When client uploads file to IIS, ASP.NET first creates the file in a the temporary upload directory. Only when the entire file is uploaded to server you can read its content from stream. By default ASP.NET uploads files to %FrameworkInstallLocation%\Temporary ASP.NET Files folder. You must make sure you have enough disk space to keep temporary files uploaded to your server. To change this folder location add the following section to your web.config file:
<configuration>
<system.web>
...
<compilation tempDirectory="temporary files directory" />
...
</system.web>
</configuration>
The code below is part of 'WebDAVServer.NtfsStorage' sample provided with the SDK.
public virtual bool Write(Stream content, string contentType, long startIndex, long totalFileSize) { RequireHasToken(); //Set timeout to maximum value to be able to upload large files. HttpContext.Current.Server.ScriptTimeout = int.MaxValue; return context.FileOperation<bool>( this, () => writeInternal(content, startIndex, totalFileSize), Privilege.Write); } private bool writeInternal(Stream content, long startIndex, long totalLength) { if (startIndex == 0 && fileInfo.Length > 0) { SafeNativeMethods.TruncateFile(fileInfo); } RewriteStream("SerialNumber", GetStreamAndDeserialize<int>("SerialNumber", context.Logger) + 1); using (var fileStream = fileInfo.Open(FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read)) { if (fileStream.Length < startIndex) { throw new DavException("Previous piece of file was not uploaded.", DavStatus.PRECONDITION_FAILED); } fileStream.Seek(startIndex, SeekOrigin.Begin); var buffer = new byte[bufSize]; int lastBytesRead; try { while ((lastBytesRead = content.Read(buffer, 0, bufSize)) > 0) { fileStream.Write(buffer, 0, lastBytesRead); fileStream.Flush(); } } catch (HttpException) { // The remote host closed the connection (for example Cancel or Pause pressed). } } return true; }