Namespace: ITHit.WebDAV.Server
Task<bool> WriteAsync( Stream content, string contentType, long startIndex, long totalFileSize )
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.FileSystemStorage.AspNet' C# & VB samples provided with the SDK.
public virtual async Task<bool> WriteAsync(Stream content, string contentType, long startIndex, long totalFileSize) { await RequireHasTokenAsync(); //Set timeout to maximum value to be able to upload large files. HttpContext.Current.Server.ScriptTimeout = int.MaxValue; if (startIndex == 0 && fileInfo.Length > 0) { using (FileStream filestream = fileInfo.Open(FileMode.Truncate)) { } } await fileInfo.SetExtendedAttributeAsync("TotalContentLength", (object)totalFileSize); await fileInfo.SetExtendedAttributeAsync("SerialNumber", ++this.serialNumber); using (FileStream 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); } if (!content.CanRead) { return true; } fileStream.Seek(startIndex, SeekOrigin.Begin); byte[] buffer = new byte[bufSize]; int lastBytesRead; try { lastBytesRead = await content.ReadAsync(buffer, 0, bufSize); while (lastBytesRead > 0) { await fileStream.WriteAsync(buffer, 0, lastBytesRead); await fileStream.FlushAsync(); lastBytesRead = await content.ReadAsync(buffer, 0, bufSize); } } catch (HttpException) { // The remote host closed the connection (for example Cancel or Pause pressed). } } await context.socketService.NotifyRefreshAsync(GetParentPath(Path)); return true; }