Writes the content of the resource to the specified stream.
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.
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();
}
IResource Interface | ITHit.WebDAV.Server Namespace