Nl.fileid =Int32.Parse (ID); The corresponding ID stream stream on the server=Lawsuit.download (NL);//stream data on the server Response.Buffer=false; Response.AddHeader ("Connection","keep-alive"); Response.ContentType="Application/octet-stream"; Response.AddHeader ("content-disposition","attachment;filename="+ URL);//here the filename if is Chinese in the browser default is the chip, should add Httputility.urlencode (filename) URL: The server address and name to download the file while(true)//if the file is larger than the buffer, the file is loaded multiple times through the while Loop { //open up buffer space byte[] buffer =New byte[1024x768];//reading data from a file intLeng = stream. Read (Buffer,0,1024x768); if(Leng = =0)//to the end of the file. Break; if(Leng = =1024x768)//the length of the read file data is equal to the buffer length, and the buffer data is written directlyResponse.BinaryWrite (buffer);//send a data flow to a client Else { //read out file data smaller than buffer, redefine buffer size, only for reading the last chunk of the file byte[] B =New byte[Leng]; for(inti =0; I < Leng; i++) B[i]=Buffer[i]; Response.BinaryWrite (b); }} stream. Close ();//Close the download fileResponse.End ();//End File Download
Source Address: http://www.cnblogs.com/chaoa/archive/2012/03/09/2386106.html
ASP. NET MVC downloads files with stream