Asp.net downloads small files

Source: Internet
Author: User

 

1 private string filename = string. empty;
2
3 protected void page_load (Object sender, eventargs E)
4 {
5 If (! Ispostback)
6 {
7 if (request. querystring ["FILENAME"]! = NULL)
8 {
9 filename = httputility. urldecode (request. querystring ["FILENAME"]. tostring ());
10 download (filename );
11}
12}
13}
14
15
16
17/** // <summary>
18 // download file
19 /// </Summary>
20 /// <Param name = "filepath"> the file path. </param>
21 private void download (string filepath)
22 {
23 response. Clear ();
24 string filename = path. getfilename (filepath );
25
26 filestream FS = new filestream (filepath, filemode. Open );
27 byte [] bytes = new byte [(INT) fs. Length];
28 FS. Read (bytes, 0, bytes. Length );
29 fs. Close ();
30 response. contenttype = "application/octet-stream ";
31 response. addheader ("content-disposition", "attachment; filename =" + httputility. urldecode (filename, system. Text. encoding. utf8 ));
32 response. binarywrite (bytes );
33 response. Flush ();
34 response. End ();
35} response. addheader ("content-disposition", "attachment; filename =" + httputility. urldecode (filename, system. Text. encoding. utf8 ));

 

The downloaded file name is garbled:

If the file name is garbled, try the following:
String httpheader = "attachment; filename =" + httputility. urlencode (strfilename + ". xls ");
Response. appendheader ("content-disposition", httpheader );
If the file content is garbled, try the following:
Response. contentencoding = system. Text. encoding. default;
Or
Response. contentencoding = system. Text. encoding. getencoding ("gb2312 ");
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.