Click or drag to resize

IContentRead Method

IT Hit WebDAV Classes Reference
Reads the file content from the repository and writes it to the specified stream.

Namespace: ITHit.WebDAV.Server
Assembly: ITHit.WebDAV.Server (in ITHit.WebDAV.Server.dll) Version: 4.5.3121.0
Syntax
void Read(
	Stream output,
	long startIndex,
	long count
)

Parameters

output
Type: System.IOStream
Output stream.
startIndex
Type: SystemInt64
The zero-bazed byte offset in file content at which to begin copying bytes to the output stream.
count
Type: SystemInt64
The number of bytes to be written to the output stream.

Return Value

Type: 
.
Exceptions
ExceptionCondition
NeedPrivilegesExceptionThe user doesn't have enough privileges.
DavExceptionIn other cases.
Remarks

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.

Examples

The code below is part of 'WebDAVServer.NtfsStorage' sample provided with the SDK.

public virtual void Read(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(context, Name);
    }

    context.FileOperation(
       this,
       () => readInternal(output, startIndex, count),
       Privilege.Read);
}

private void readInternal(Stream output, long startIndex, long count)
{
    var buffer = new byte[bufSize];
    using (var fileStream = fileInfo.Open(FileMode.Open, FileAccess.Read, FileShare.Read))
    {
        fileStream.Seek(startIndex, SeekOrigin.Begin);
        int bytesRead;
        var toRead = (int)Math.Min(count, bufSize);

        try
        {
            while (toRead > 0 && (bytesRead = fileStream.Read(buffer, 0, toRead)) > 0)
            {
                output.Write(buffer, 0, bytesRead);
                count -= bytesRead;
            }
        }
        catch (HttpException)
        {
            // The remote host closed the connection (for example Cancel or Pause pressed).
        }
    }
}
See Also