Public bool down (string filepath, string code)
{
Bool succ = false;
System. Io. Stream istream = NULL;
// Buffer to read 10 K bytes in Chunk:
Byte [] buffer = new byte [10000];
// Length of the file:
Int length;
// Total Bytes to read:
Long datatoread;
// Identify the file to download including its path.
// String filepath = "downloadfilename ";
// Identify the file name.
String filename = system. Io. Path. getfilename (filepath );
Try
{
// Open the file.
Istream = new system. Io. filestream (filepath, system. Io. filemode. Open,
System. Io. fileaccess. Read, system. Io. fileshare. Read );
// Total Bytes to read:
Datatoread = istream. length;
Response. contenttype = "application/octet-stream ";
Response. addheader ("content-disposition", "attachment; filename =" + filename );
// Read the bytes.
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;
}
}
}
Catch (exception ex)
{
// Trap the error, if any.
Response. Write ("error:" + ex. Message );
}
Finally
{
If (istream! = NULL)
{
// Close the file.
Istream. Close ();
}
}
Return succ;
}