Copy Code code as follows:
Using System;
Using System.Data;
Using System.Configuration;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Web.UI.HtmlControls;
Using System.IO;
public partial class _default:system.web.ui.page
{
protected void Page_Load (object sender, EventArgs e)
{
}
TransmitFile implementation Download
protected void Button1_Click (object sender, EventArgs e)
{
Response.ContentType = "application/x-zip-compressed";
Response.AddHeader ("Content-disposition", "Attachment;filename=z.zip");
string filename = Server.MapPath ("Download/z.zip");
Response.TransmitFile (filename);
}
WriteFile Implementation Download
protected void button2_click (object sender, EventArgs e)
{
string filename = "Asd.txt";//client saved file name
String Filepath=server.mappath ("Download/aaa.txt");//path
FileInfo FileInfo = new FileInfo (FilePath);
Response.Clear ();
Response.clearcontent ();
Response.ClearHeaders ();
Response.AddHeader ("Content-disposition", "attachment;filename=" + filename);
Response.AddHeader ("Content-length", fileInfo.Length.ToString ());
Response.AddHeader ("content-transfer-encoding", "binary");
Response.ContentType = "Application/octet-stream";
response.contentencoding = System.Text.Encoding.GetEncoding ("gb2312");
Response.WriteFile (Fileinfo.fullname);
Response.Flush ();
Response.End ();
}
WriteFile-Block Download
protected void Button3_Click (object sender, EventArgs e)
{
string filename = "Aaa.txt";//client saved file name
String filePath = Server.MapPath ("Download/aaa.txt");//path
System.IO.FileInfo FileInfo = new System.IO.FileInfo (FilePath);
if (fileinfo.exists = = True)
{
Const LONG chunksize = 102400;//100k Read the file every time, read only 100K, which can ease the pressure on the server
byte[] buffer = new Byte[chunksize];
Response.Clear ();
System.IO.FileStream IStream = System.IO.File.OpenRead (FilePath);
Long Datalengthtoread = istream.length;//Gets the total size of the downloaded file
Response.ContentType = "Application/octet-stream";
Response.AddHeader ("Content-disposition", "attachment; Filename= "+ httputility.urlencode (fileName));
while (Datalengthtoread > 0 && response.isclientconnected)
{
int lengthread = istream.read (buffer, 0, Convert.ToInt32 (chunksize));//Read size
Response.OutputStream.Write (buffer, 0, lengthread);
Response.Flush ();
Datalengthtoread = Datalengthtoread-lengthread;
}
Response.close ();
}
}
Streaming mode download
protected void Button4_Click (object sender, EventArgs e)
{
string filename = "Aaa.txt";//client saved file name
String filePath = Server.MapPath ("Download/aaa.txt");//path
Downloading files as streams of characters
FileStream fs = new FileStream (FilePath, FileMode.Open);
byte[] bytes = new byte[(int) fs. Length];
Fs. Read (bytes, 0, bytes. Length);
Fs. Close ();
Response.ContentType = "Application/octet-stream";
Notifies the browser to download the file instead of opening it
Response.AddHeader ("Content-disposition", "attachment; Filename= "+ httputility.urlencode (FileName, System.Text.Encoding.UTF8));
Response.BinaryWrite (bytes);
Response.Flush ();
Response.End ();
}
}