We have an ASP.NET/C# 2.0 (.NET 2.0) web application.
To make the client to download a byte array programmatically we have a piece of code which looks like this:
Response.ContentType = contentType ?? "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
Stream iStream = ...
byte[] buffer = new Byte[10000];
while (dataToRead > 0)
{
// Verify that the client is connected.
if (response.IsClientConnected)
{
// Read the data in buffer.
length = iStream.Read(buffer, 0, 10000);
// Write the data to the current output stream.
response.OutputStream.Write(buffer, 0, length);
// Flush the data to the HTML output.
response.Flush();
//buffer= new Byte[10000];
dataToRead = dataToRead - length;
}
else
{
//prevent infinite loop if user disconnects
dataToRead = -1;
}
}
Response.Flush();
Response.Close();
This works on all our installations but one (on IIS 6) where I got files truncated (about 30KB). Did you ever experience such a problem?
The problem was related to a module. We solved with
Response.End()to prevent the execution of the module.