ASP. NET MVC downloads files with stream

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.