Click or drag to resize

IContentReadAsync Method

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

Namespace:  ITHit.Server
Assembly:  ITHit.Server (in ITHit.Server.dll) Version: 13.3.13068
Syntax
Task ReadAsync(
	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: Task
.
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.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).
        }
    }
}
Examples

The code below is part of 'WebDAVServer.FileSystemSynchronization.AspNetCore' C# & VB samples provided with the SDK.

C#
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).
        }
    }
}
See Also