1 public class Download
2 {
3
4 private static ILog log = LogManager.GetLogger(typeof(ConfigManager)); //readonly
5
6
7 public Download()
8 {
9 }
10
11 public static bool ResponseFile(string fullPath)
12 {
13 HttpRequest request = HttpContext.Current.Request;
14 HttpResponse response = HttpContext.Current.Response;
15 try
16 {
17 FileInfo info = new FileInfo(fullPath);
18 if (info.Exists)
19 {
20 string name = info.Name;
21 string extension = info.Extension;
22 name = name.Substring(0, name.Length - extension.Length);
23 while (HttpUtility.UrlEncode(name + extension, Encoding.UTF8).Length > 0x9c)
24 {
25 name = name.Substring(0, name.Length - 1);
26 }
27 name = HttpUtility.UrlEncode(name + extension, Encoding.UTF8);
28 response.Clear();
29 if (info.Length < 1024000)//0xfa000L)
30 {
31 response.ContentType = "application/octet-stream";
32 response.AddHeader("Content-Disposition", "attachment;filename=" + name);
33 response.WriteFile(info.FullName);
34 }
35 else
36 {
37 FileStream input = new FileStream(fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
38 BinaryReader reader = new BinaryReader(input);
39 try
40 {
41 long length = input.Length;
42 long num2 = 0L;
43 int count = 10240;// 0x2800;
44 if (request.Headers["Range"] != null)
45 {
46 response.StatusCode = 0xce;
47 num2 = Convert.ToInt64(request.Headers["Range"].Split(new char[] { '=', '-' })[1]);
48 }
49 response.AddHeader("Content-Length", (length - num2).ToString());
50 if (num2 != 0L)
51 {
52 response.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", num2, length - 1L, length));
53 }
54 response.ContentType = "application/octet-stream";
55 response.AddHeader("Content-Disposition", "attachment;filename=" + name);
56 reader.BaseStream.Seek(num2, SeekOrigin.Begin);
57 int num4 = ((int)Math.Floor((double)(((double)(length - num2)) / ((double)count)))) + 1;
58 for (int i = 0; i < num4; i++)
59 {
60 if (response.IsClientConnected)
61 {
62 response.BinaryWrite(reader.ReadBytes(count));
63 response.Flush();
64 }
65 else
66 {
67 i = num4;
68 }
69 }
70 }
71 catch
72 {
73 return false;
74 }
75 finally
76 {
77 reader.Close();
78 input.Close();
79 }
80 }
81 }
82 }
83 catch (Exception exception)
84 {
85 if (log.IsErrorEnabled)
86 {
87 log.Error("", exception);
88 }
89 return false;
90 }
91 return true;
92 }
93
94 }