Copy Code code as follows:
public void Filedownloaddel (string fullfilename)
{
System.IO.Stream iStream = null;
Buffer to read 10K 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's 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 (); No this will append the refreshed contents of the page to the file.
}
catch (Exception ex)
{
Trap the error, if any.
Response.Write ("Error:" + ex.) message);
Base. Writelog ("Information", "Download Information:" + ex.) Message + '! ', Logtype.error, this. GetType (). ToString ());
}
Finally
{
if (IStream!= null)
{
Close the file.
Istream.close ();
}
File.delete (Fullfilename);
}
}