ASP. NET File Download-use stream output

Source: Internet
Author: User
ASP. NET File Download-use stream output

 

/// <Summary>
/// Download an object
/// Use stream output
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Private void button#click (Object sender, system. eventargs E)
{
String sfilename = server. mappath ("./commonly used phone number for work. xls ");
Filestream = new filestream (sfilename, filemode. Open );
Long filesize = filestream. length;
Byte [] filebuffer = new byte [filesize];
Filestream. Read (filebuffer, 0, (INT) filesize );
// If you do not write the filestream. Close () statement, you cannot download the file again if you cancel the file during the download process.
Filestream. Close ();

Context. response. contenttype = "application/octet-stream ";
Context. response. appendheader ("content-disposition", "attachment; filename =" + httputility. urlencode (sfilename, encoding. utf8 ));
Context. response. addheader ("Content-Length", filesize. tostring ());

Context. response. binarywrite (filebuffer );
Context. response. End ();
Context. response. Close ();
}

/// <Summary>
/// Download an object
/// Direct file output
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Private void button2_click (Object sender, system. eventargs E)
{
String savefilename = "Commonly Used Telephone Number .xls ";

Response. Clear ();
Response. Buffer = true;
// Response. charset = "UTF-8"; // optional?

// The following two statements have one meaning. I don't know why people on the internet want to add this!
// This. enableviewstate = false;
Enableviewstate = false;

// Response. contenttype = "application/MS-excel"; // optional?
// Response. contentencoding = system. Text. encoding. utf8; // is it optional?

// Directly write down the following statement. The file name displayed on the client is garbled.
// Response. appendheader ("content-disposition", "attachment; filename =" + savefilename );
// It must be written as this
Response. appendheader ("content-disposition", "attachment; filename =" + httputility. urlencode (savefilename, encoding. utf8 ));

Response. writefile (savefilename );
Response. Flush ();

Response. End (); // What is the order of end and close? During the test, after the two positions are arranged and exchanged, the execution is not affected.
Response. Close ();
}

 

/// <Summary>
/// Download an object
/// Use stream output
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Private void button#click (Object sender, system. eventargs E)
{
String sfilename = server. mappath ("./commonly used phone number for work. xls ");
Filestream = new filestream (sfilename, filemode. Open );
Long filesize = filestream. length;
Byte [] filebuffer = new byte [filesize];
Filestream. Read (filebuffer, 0, (INT) filesize );
// If you do not write the filestream. Close () statement, you cannot download the file again if you cancel the file during the download process.
Filestream. Close ();

Context. response. contenttype = "application/octet-stream ";
Context. response. appendheader ("content-disposition", "attachment; filename =" + httputility. urlencode (sfilename, encoding. utf8 ));
Context. response. addheader ("Content-Length", filesize. tostring ());

Context. response. binarywrite (filebuffer );
Context. response. End ();
Context. response. Close ();
}

/// <Summary>
/// Download an object
/// Direct file output
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Private void button2_click (Object sender, system. eventargs E)
{
String savefilename = "Commonly Used Telephone Number .xls ";

Response. Clear ();
Response. Buffer = true;
// Response. charset = "UTF-8"; // optional?

// The following two statements have one meaning. I don't know why people on the internet want to add this!
// This. enableviewstate = false;
Enableviewstate = false;

// Response. contenttype = "application/MS-excel"; // optional?
// Response. contentencoding = system. Text. encoding. utf8; // is it optional?

// Directly write down the following statement. The file name displayed on the client is garbled.
// Response. appendheader ("content-disposition", "attachment; filename =" + savefilename );
// It must be written as this
Response. appendheader ("content-disposition", "attachment; filename =" + httputility. urlencode (savefilename, encoding. utf8 ));

Response. writefile (savefilename );
Response. Flush ();

Response. End (); // What is the order of end and close? During the test, after the two positions are arranged and exchanged, the execution is not affected.
Response. Close ();
}

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.