Asp.net supports Chinese name file download code

Source: Internet
Author: User
Tags flush httpcontext rar

There are already many questions about Chinese file downloading and online consultation and Q & A. The original download code is as follows:

Response. setheader ("content-disposition", "attachment; filename =" + java.net. urlencoder. encode (filename, "UTF-8 "));
The downloaded program contains this sentence. Generally, the file name is correctly displayed in the ie6 download prompt box, whether in simplified Chinese or Japanese. However, we did not carefully test the Chinese file name with a long file name. Now, after careful testing, we found that the text cannot be downloaded as long as it contains more than 17 characters. After some good google and repeated tests, I finally got a systematic understanding of this problem.


Public partial class filedownload: system. web. ui. page
{
// Provide the downloaded file. If it is not encoded, the file name will be garbled.
Private string filename = httpcontext. current. server. urlencode ("2.16.rar ");
Private string filepath = httpcontext. current. server. mappath ("mirror.rar ");
// Use transmiffile to download an object
Protected void btndl1_click (object sender, eventargs e)
{
Fileinfo info = new fileinfo (filepath );
Long filesize = info. length;
Response. clear ();
Response. contenttype = "application/x-zip-compressed ";
Response. addheader ("content-disposition", "attachment; filename =" + filename );
// If content-length is flush, the download progress is not displayed.
Response. addheader ("content-length", filesize. tostring ());
Response. transmitfile (filepath, 0, filesize );
Response. flush ();
Response. close ();
}

// Use writefile to download an object
Protected void btndl2_click (object sender, eventargs e)
{
Fileinfo info = new fileinfo (filepath );
Long filesize = info. length;
Response. clear ();
Response. contenttype = "application/octet-stream ";
Response. addheader ("content-disposition", "attachement; filename =" + filename );
// Specify the file size
Response. addheader ("content-length", filesize. tostring ());
Response. writefile (filepath, 0, filesize );
Response. flush ();
Response. close ();
}

// Use outputstream. write to download objects in multiple parts
Protected void btndl3_click (object sender, eventargs e)
{
// Specify the block size
Long chunksize = 102400;
// Create a K buffer
Byte [] buffer = new byte [chunksize];
// Number of read bytes
Long datatoread = 0;
Filestream stream = null;
Try
{
// Open the file
Stream = new filestream (filepath, filemode. open, fileaccess. read, fileshare. read );
Datatoread = stream. length;

// Add an http header
Response. contenttype = "application/octet-stream ";
Response. addheader ("content-disposition", "attachement; filename =" + filename );
Response. addheader ("content-length", datatoread. tostring ());

While (datatoread> 0)
{
If (response. isclientconnected)
{
Int length = stream. read (buffer, 0, convert. toint32 (chunksize ));
Response. outputstream. write (buffer, 0, length );
Response. flush ();
Response. clear ();
Datatoread-= length;
}
Else
{
// Prevent client from losing connection
Datatoread =-1;
}
}
}
Catch (exception ex)
{
Response. write ("error:" + ex. message );
}
Finally
{
If (stream! = Null)
{
Stream. close ();
}
Response. close ();
}
}

// Use binarywrite to download files, which is inefficient at large files
Protected void btndl4_click (object sender, eventargs e)
{
Filestream stream = null;
Try
{
// Read the file. A large file occupies a large amount of memory for one read.
Stream = new filestream (filepath, filemode. open, fileaccess. read, fileshare. read );
Byte [] bytes = new byte [stream. length];
Stream. read (bytes, 0, bytes. length );
Stream. close ();

// Add an http header
Response. contenttype = "application/octet-stream ";
Response. addheader ("content-disposition", "attachement; filename =" + filename );
Response. addheader ("content-length", bytes. length. tostring ());
Response. binarywrite (bytes );
Response. flush ();
}
Catch (exception ex)
{
Response. write ("error:" + ex. message );
}
Finally
{
If (stream! = Null)
{
Stream. close ();
}
Response. close ();
}
}
// Use binarywrite to download objects in multiple parts
Protected void btndl5_click (object sender, eventargs e)
{
// Specify the block and buffer
Long chunksize = 102400;
Byte [] buffer = new byte [chunksize];
Filestream stream = null;
Long datatoread = 0;
Try
{
Stream = new filestream (filepath, filemode. open, fileaccess. read, fileshare. read );
Datatoread = stream. length;
// Add an http header
Response. contenttype = "application/octet-stream ";
Response. addheader ("content-disposition", "attachement; filename =" + filename );
Response. addheader ("content-length", datatoread. tostring ());

While (datatoread> 0)
{
If (response. isclientconnected)
{
Int length = stream. read (buffer, 0, convert. toint32 (chunksize ));
Response. binarywrite (buffer );
Response. flush ();
Response. clear ();

Datatoread-= length;
}
Else
{
Datatoread =-1;
}
}

}
Catch (exception ex)
{
Response. write ("error:" + ex. message );
}
Finally
{
If (stream! = Null)
{
Stream. close ();
}
Response. close ();
}
}
}

Related Article

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.