IResource.WriteToStream Method 

IT Hit WebDAV for .NET

Writes the content of the resource to the specified stream.

[Visual Basic]
Function WriteToStream( _
   ByVal output As Stream, _
   ByVal startIndex As Long, _
   ByVal count As Long _
) As WebDAVResponse
[C#]
WebDAVResponse WriteToStream(
   Stream output,
   long startIndex,
   long count
);

Parameters

output
Output stream.
startIndex
The zero-bazed byte offset in resource content at which to begin copying bytes to the output stream.
count
The number of bytes to be written to the output stream.

Return Value

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 resource specifying Range header. Download managers may use this header to download single file using several threads at a time.

Example

    public WebDAVResponse WriteToStream(Stream output, long byteStart, long count)
    {
        System.Web.HttpContext.Current.Server.ScriptTimeout = 1200;

        SqlConnection conn = new SqlConnection(connStr);
        SqlCommand cmd;
        SqlDataReader reader = null;
        conn.Open();

        try
        {
            cmd = conn.CreateCommand();
            cmd.CommandText = "SELECT Content FROM Repository WHERE ID = @ID";
            cmd.Parameters.Add("@ID", SqlDbType.Int).Value = ID;
        
            reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess);
            reader.Read();
            
            long bufSize = 1048576; // 1Mb
            byte[] buf = new byte[bufSize];
            long retval;
            long startIndex = byteStart;
            
            while ((retval = reader.GetBytes(reader.GetOrdinal("Content"), 
                startIndex, buf, 0, (int)(count>bufSize ? bufSize : count) )) > 0)
            {
                try
                {
                    output.Write(buf, 0, (int)retval);
                }
                catch (System.Web.HttpException)
                { // The remote host closed the connection
                    return new OkResponse();
                }
                startIndex += retval;
                count-=retval;
            }
        }
        finally
        {
            if(reader != null) reader.Close();
            conn.Close();
        }
        
        return new OkResponse();
    }
 

See Also

IResource Interface | ITHit.WebDAV.Server Namespace