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 ");