One//transmitfile Implementation Download protected voidButton1_Click (Objectsender, EventArgs e) { /*Microsoft has provided a new method for response objects TransmitFile to solve the problem of Aspnet_wp.exe process recycling that could not be successfully downloaded when using Response.BinaryWrite to download files over 400MB Problem. The code is as follows:*/Response.ContentType="application/x-zip-compressed"; Response.AddHeader ("content-disposition","Attachment;filename=z.zip"); stringfilename = Server.MapPath ("Download/z.zip"); Response.TransmitFile (filename); } Two,//WriteFile Implementation Download protected voidButton2_Click (Objectsender, EventArgs e) { /*using System.IO; */ stringFileName ="Asd.txt";//file name saved by the client stringFilePath = Server.MapPath ("Download/aaa.txt");//PathFileInfo FileInfo=NewFileInfo (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 (); } Third,//WriteFile sub-block download protected voidButton3_Click (Objectsender, EventArgs e) { stringFileName ="Aaa.txt";//file name saved by the client stringFilePath = Server.MapPath ("Download/aaa.txt");//PathSystem.IO.FileInfo FileInfo=NewSystem.IO.FileInfo (FilePath); if(Fileinfo.exists = =true) { Const LongChunkSize =102400;//100K Read the file every time, only read 100K, this can alleviate the pressure of the server byte[] buffer =New byte[ChunkSize]; Response.Clear (); System.IO.FileStream IStream=System.IO.File.OpenRead (FilePath); LongDatalengthtoread = Istream.length;//get the total size of the downloaded fileResponse.ContentType ="Application/octet-stream"; Response.AddHeader ("content-disposition","attachment; Filename="+Httputility.urlencode (fileName)); while(Datalengthtoread >0&&response.isclientconnected) {intLengthread = istream.read (buffer,0, Convert.ToInt32 (ChunkSize));//the size of the readResponse.OutputStream.Write (Buffer,0, Lengthread); Response.Flush (); Datalengthtoread= Datalengthtoread-Lengthread; } response.close (); }} four,//Stream mode Download protected voidButton4_Click (Objectsender, EventArgs e) { stringFileName ="Aaa.txt";//file name saved by the client stringFilePath = Server.MapPath ("Download/aaa.txt");//Path//download a file as a stream of charactersFileStream fs =NewFileStream (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 a file instead of opening itResponse.AddHeader ("content-disposition","attachment; Filename="+Httputility.urlencode (FileName, System.Text.Encoding.UTF8)); Response.BinaryWrite (bytes); Response.Flush (); Response.End (); }//---------------------------------------------------------- Public voidDownloadFile (System.Web.UI.Page webform,string filenamewhenuserdownload, String filebody) { WebForm.Response.ClearHeaders (); WebForm.Response.Clear (); WebForm.Response.Expires=0; WebForm.Response.Buffer=true; WebForm.Response.AddHeader ("Accept-language","ZH-TW"); //' File nameWebForm.Response.AddHeader ("content-disposition","attachment; Filename= '"+system.web.httputility.urlencode (Filenamewhenuserdownload, System.Text.Encoding.UTF8) +"'"); WebForm.Response.ContentType="Application/octet-stream"; //' File contentsWebForm.Response.Write (Filebody);//-----------WebForm.Response.End ();}//The above code is to download a dynamically generated text file, if the file already exists on the server side of the entity path, you can pass the following function: Public voidDownloadfilebyfilepath (System.Web.UI.Page webform,string filenamewhenuserdownload, String FilePath) { WebForm.Response.ClearHeaders (); WebForm.Response.Clear (); WebForm.Response.Expires=0; WebForm.Response.Buffer=true; WebForm.Response.AddHeader ("Accept-language","ZH-TW"); //file nameWebForm.Response.AddHeader ("content-disposition","attachment; Filename= '"+ System.Web.HttpUtility.UrlEncode (filenamewhenuserdownload, System.Text.Encoding.UTF8) +"'" ); WebForm.Response.ContentType="Application/octet-stream"; //File ContentsWebForm.Response.Write (System.io.file.rea}dallbytes (FilePath));//---------WebForm.Response.End ();}
C # Download the file code from the server