The previous blog also said that the next C # in the export of Excel through XML, these file operations are very useful, the following is the download of the file, before the project is to write it in the space log, there is time to get them all out
Let's put the code in question.
usingSystem;usingSystem.Data;usingSystem.Configuration;usingSystem.Collections;usingsystem.web;usingSystem.Web.Security;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Web.UI.WebControls.WebParts;usingSystem.Web.UI.HtmlControls;usingSystem.IO;namespaceinfoplatclient.netdisk{ Public Partial classDownLoad:Com.DRPENG.Common.WebStruct.BaseForm {/// <summary> ///get the path to the file you want to download/// </summary> Private stringFilerpath {Get { returnrequest["Filerpath"] ==NULL?"": request["Filerpath"]; } } /// <summary> ///get the name of the file you want to download/// </summary> protected voidPage_Load (Objectsender, EventArgs e) { if(!IsPostBack) This. DownloadFile (); } Public voidDownloadFile () {response.clearheaders (); Response.Clear (); Response.Expires=0; Response.Buffer=true; Response.AddHeader ("Accept-language","ZH-TW"); stringName =System.IO.Path.GetFileName (Filerpath); System.IO.FileStream Files=NewFileStream (Filerpath, FileMode.Open, FileAccess.Read, FileShare.Read); byte[] bytefile=NULL; if(Files. Length = =0) {Bytefile=New byte[1]; } Else{bytefile=New byte[Files. Length]; } files. Read (Bytefile,0, (int) bytefile.length); Files. Close (); Response.AddHeader ("content-disposition","attachment;filename="+Httputility.urlencode (name, System.Text.Encoding.UTF8)); Response.ContentType="APPLICATION/OCTET-STREAM;CHARSET=GBK"; Response.BinaryWrite (Bytefile); Response.End (); }}} has been used this way, but one time the user uploaded a 700Mb file times memory overflow problem, analyzed the reason, the user's memory only 256M, and download the file to create a memory stream, resulting in memory overflow. Solution:1>WriteFile Sub-block download, that is, each time you download a specified number of pieces; 2>by means of hyperlinks; Lbldownload.text="<a href= '"+ drv["VPath"]. ToString () +"' > Downloads </a>"Here are four ways to implement file downloads:usingSystem;usingSystem.Data;usingSystem.Configuration;usingsystem.web;usingSystem.Web.Security;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Web.UI.WebControls.WebParts;usingSystem.Web.UI.HtmlControls;usingSystem.IO; Public Partial class_default:system.web.ui.page {protected voidPage_Load (Objectsender, EventArgs e) { } //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); } //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 (); } //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 (); } } //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 (); }}
C # Several ways to implement file downloads