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 ();
}