Copy codeThe Code is as follows:
Public void FileDownLoadDel (string fullFilename)
{
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 = fullFilename;
Filepath = Server. MapPath (filepath );
// 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;
Response. Clear ();
}
}
Response. End (); // The content refreshed on the page is appended to the file.
}
Catch (Exception ex)
{
// Trap the error, if any.
// Response. Write ("Error:" + ex. Message );
// Base. WriteLog ("", ":" + ex. Message + "! ", LogType. Error, this. GetType (). ToString ());
}
Finally
{
If (iStream! = Null)
{
// Close the file.
IStream. Close ();
}
File. Delete (fullFilename );
}
}