Namespace: ITHit.Server
Exception | Condition |
---|---|
NeedPrivilegesException | The user doesn't have enough privileges. |
DavException | In other cases. |
By default ASP.NET buffers content on server side before sending output. You must turn off buffering to eliminate keeping entire file content in memory before sending:
HttpContext.Current.Response.BufferOutput = false;
Client application can request only a part of a file specifying Range header. Download managers may use this header to download single file using several threads at a time.
The code below is part of 'WebDAVServer.FileSystemStorage.AspNet' C# & VB samples provided with the SDK.
public virtual async Task ReadAsync(Stream output, long startIndex, long count) { //Set timeout to maximum value to be able to download large files. HttpContext.Current.Server.ScriptTimeout = int.MaxValue; if (ContainsDownloadParam(context.Request.RawUrl)) { AddContentDisposition(Name); } byte[] buffer = new byte[bufSize]; using (FileStream fileStream = fileInfo.Open(FileMode.Open, FileAccess.Read, FileShare.Read)) { fileStream.Seek(startIndex, SeekOrigin.Begin); int bytesRead; int toRead = (int)Math.Min(count, bufSize); if (toRead <= 0) { return; } try { bytesRead = await fileStream.ReadAsync(buffer, 0, toRead); while (bytesRead > 0 && count > 0) { await output.WriteAsync(buffer, 0, bytesRead); count -= bytesRead; bytesRead = await fileStream.ReadAsync(buffer, 0, toRead); } } catch (HttpException) { // The remote host closed the connection (for example Cancel or Pause pressed). } } }
The code below is part of 'WebDAVServer.FileSystemSynchronization.AspNetCore' C# & VB samples provided with the SDK.
public virtual async Task ReadAsync(Stream output, long startIndex, long count) { if (ContainsDownloadParam(context.Request.RawUrl)) { AddContentDisposition(Name); } byte[] buffer = new byte[bufSize]; await using (FileStream fileStream = fileInfo.Open(FileMode.Open, FileAccess.Read, FileShare.Read)) { fileStream.Seek(startIndex, SeekOrigin.Begin); int bytesRead; int toRead = (int)Math.Min(count, bufSize); if (toRead <= 0) { return; } try { bytesRead = await fileStream.ReadAsync(buffer, 0, toRead); while (bytesRead > 0 && count > 0) { await output.WriteAsync(buffer, 0, bytesRead); count -= bytesRead; bytesRead = await fileStream.ReadAsync(buffer, 0, toRead); } } catch (IOException) { // The remote host closed the connection (for example Cancel or Pause pressed). } } }