1 /// <summary>
2 // download the transmitfile
3 /// </Summary>
4 /// <Param name = "filepath"> </param>
5 public static void downloadfile (string filepath)
6 {
7 string filename = httpcontext. Current. server. mappath (filepath );
8 var response = httpcontext. Current. response;
9 response. contenttype = "application/X-zip-compressed ";
10 response. addheader ("content-disposition", "attachment; filename =" + path. getfilename (filename ));
11 response. transmitfile (filename );
12}
13
14 /// <summary>
15 // download large files
16 /// </Summary>
17 /// <Param name = "filepath"> </param>
18 public static void downloadbigfile (string filepath)
19 {
20 filepath = httpcontext. Current. server. mappath (filepath); // path
21 string filename = path. getfilename (filepath); // file name saved by the client
22 var response = httpcontext. Current. response;
23 system. Io. fileinfo = new system. Io. fileinfo (filepath );
24 if (fileinfo. exists = true)
25 {
26 const long chunksize = 102400; // 100 K read only 100 k each time, which can relieve the pressure on the server
27
28 byte [] buffer = new byte [chunksize];
29
30 response. Clear ();
31 system. Io. filestream istream = system. Io. file. openread (filepath );
32 long datalengthtoread = istream. length; // obtain the total size of the downloaded file
33 response. contenttype = "application/octet-stream ";
34 response. addheader ("content-disposition", "attachment; filename =" + httputility. urlencode (filename ));
35 while (datalengthtoread> 0 & response. isclientconnected)
36 {
37 // read size response. outputstream. Write (buffer, 0, lengthread );
38 int lengthread = istream. Read (buffer, 0, convert. toint32 (chunksize ));
39 response. Flush ();
40 datalengthtoread = datalengthtoread-lengthread;
41}
42
43 response. Close ();
44}
45}
46
47 // <summary>
48 // stream download
49 // </Summary>
50 /// <Param name = "filepath"> </param>
51 public static void downloadfilestream (string filepath)
52 {
53 filepath = httpcontext. Current. server. mappath (filepath); // path
54 string filename = path. getfilename (filepath); // file name saved by the client
55 var response = httpcontext. Current. response;
56 filestream FS = new filestream (filepath, filemode. Open );
57
58 byte [] bytes = new byte [(INT) fs. Length]; FS. Read (bytes, 0, bytes. Length );
59
60 fs. Close ();
61 response. contenttype = "application/octet-stream ";
62 // notify the browser to download the file instead of opening it
63 response. addheader ("content-disposition", "attachment; filename =" + httputility. urlencode (filename, system. Text. encoding. utf8 ));
64 response. binarywrite (bytes );
65 response. Flush ();
66 response. End ();
67}
68
69 /// <summary>
70 // Download text as an object
71 /// </Summary>
72 /// <Param name = "text"> </param>
73 // <Param name = "FILENAME"> </param>
74 public static void downloadtext (string text, string filename)
75 {
76 byte [] bytes = encoding. Default. getbytes (text );
77 var response = httpcontext. Current. response;
78 response. contenttype = "application/octet-stream ";
79 // notify the browser to download the file instead of opening it
80 response. addheader ("content-disposition", "attachment; filename =" + httputility. urlencode (filename, system. Text. encoding. utf8 ));
81 response. binarywrite (bytes );
82 response. Flush ();
83 response. End ();
84}