Using System. IO
Void Download ()
{
System. IO. FileStream r = new System. IO. FileStream (FileName, System. IO. FileMode. Open); // File Download instantiation
// Set basic information
Response. Buffer = false;
Response. AddHeader ("Connection", "Keep-Alive ");
Response. ContentType = "application/octet-stream ";
Response. addHeader ("Content-Disposition", "attachment; filename =" + System. IO. path. getFileName (FileName); // if the file name is a Chinese character, it is a chip by default in the browser. You should add HttpUtility. urlEncode (filename)
Response. AddHeader ("Content-Length", r. Length. ToString ());
While (true) // if the file is greater than the buffer, the file is loaded multiple times through the while LOOP
{
// Open the buffer space www.2cto.com
Byte [] buffer = new byte [1024]; // reads file data
Int leng = r. Read (buffer, 0, 1024 );
If (leng = 0) // end at the end of the file
Break;
If (leng = 1024) // read the file data length is equal to the buffer length, directly write the buffer data
Response. BinaryWrite (buffer); // send data streams to the client
Else
{
// Read the file data is smaller than the buffer, and the buffer size is redefined. It is only used to read the last data block of the file.
Byte [] B = new byte [leng]; for (int I = 0; I <leng; I ++)
B [I] = buffer [I];
Response. BinaryWrite (B );
}
}
R. Close (); // Close the downloaded file
Response. End (); // End object download
}
From hcstudio