纽威
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

192 lines
7.6 KiB

3 years ago
  1. using System;
  2. using System.IO;
  3. using System.Threading;
  4. using System.Web;
  5. namespace NFine.Code
  6. {
  7. public class FileDownHelper
  8. {
  9. public FileDownHelper()
  10. { }
  11. public static string FileNameExtension(string FileName)
  12. {
  13. return Path.GetExtension(MapPathFile(FileName));
  14. }
  15. public static string MapPathFile(string FileName)
  16. {
  17. return HttpContext.Current.Server.MapPath(FileName);
  18. }
  19. public static bool FileExists(string FileName)
  20. {
  21. string destFileName = FileName;
  22. if (File.Exists(destFileName))
  23. {
  24. return true;
  25. }
  26. else
  27. {
  28. return false;
  29. }
  30. }
  31. public static void DownLoadold(string FileName, string name)
  32. {
  33. string destFileName = FileName;
  34. if (File.Exists(destFileName))
  35. {
  36. FileInfo fi = new FileInfo(destFileName);
  37. HttpContext.Current.Response.Clear();
  38. HttpContext.Current.Response.ClearHeaders();
  39. HttpContext.Current.Response.Buffer = false;
  40. HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(name, System.Text.Encoding.UTF8));
  41. HttpContext.Current.Response.AppendHeader("Content-Length", fi.Length.ToString());
  42. HttpContext.Current.Response.ContentType = "application/octet-stream";
  43. HttpContext.Current.Response.WriteFile(destFileName);
  44. HttpContext.Current.Response.Flush();
  45. HttpContext.Current.Response.End();
  46. }
  47. }
  48. public static void DownLoad(string FileName)
  49. {
  50. string filePath = MapPathFile(FileName);
  51. long chunkSize = 204800; //指定块大小
  52. byte[] buffer = new byte[chunkSize]; //建立一个200K的缓冲区
  53. long dataToRead = 0; //已读的字节数
  54. FileStream stream = null;
  55. try
  56. {
  57. //打开文件
  58. stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
  59. dataToRead = stream.Length;
  60. //添加Http头
  61. HttpContext.Current.Response.ContentType = "application/octet-stream";
  62. HttpContext.Current.Response.AddHeader("Content-Disposition", "attachement;filename=" + HttpUtility.UrlEncode(Path.GetFileName(filePath)));
  63. HttpContext.Current.Response.AddHeader("Content-Length", dataToRead.ToString());
  64. while (dataToRead > 0)
  65. {
  66. if (HttpContext.Current.Response.IsClientConnected)
  67. {
  68. int length = stream.Read(buffer, 0, Convert.ToInt32(chunkSize));
  69. HttpContext.Current.Response.OutputStream.Write(buffer, 0, length);
  70. HttpContext.Current.Response.Flush();
  71. HttpContext.Current.Response.Clear();
  72. dataToRead -= length;
  73. }
  74. else
  75. {
  76. dataToRead = -1; //防止client失去连接
  77. }
  78. }
  79. }
  80. catch (Exception ex)
  81. {
  82. HttpContext.Current.Response.Write("Error:" + ex.Message);
  83. }
  84. finally
  85. {
  86. if (stream != null) stream.Close();
  87. HttpContext.Current.Response.Close();
  88. }
  89. }
  90. //临时文件下载,下载后删除
  91. public static void DownloadFile(string name, string FileName)
  92. {
  93. string destFileName = FileName;
  94. if (File.Exists(destFileName))
  95. {
  96. FileInfo fi = new FileInfo(destFileName);
  97. HttpContext.Current.Response.Clear();
  98. HttpContext.Current.Response.ClearHeaders();
  99. HttpContext.Current.Response.Buffer = false;
  100. HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(name, System.Text.Encoding.UTF8));
  101. HttpContext.Current.Response.AppendHeader("Content-Length", fi.Length.ToString());
  102. HttpContext.Current.Response.ContentType = "application/octet-stream";
  103. HttpContext.Current.Response.WriteFile(destFileName);
  104. HttpContext.Current.Response.Flush();
  105. File.Delete(FileName);//删除已下载文件
  106. HttpContext.Current.Response.End();
  107. }
  108. }
  109. //删除文件
  110. public static void DeleteFile( string FileName)
  111. {
  112. string destFileName = FileName;
  113. if (File.Exists(destFileName))
  114. {
  115. File.Delete(FileName);//删除 文件
  116. }
  117. }
  118. public static bool ResponseFile(HttpRequest _Request, HttpResponse _Response, string _fileName, string _fullPath, long _speed)
  119. {
  120. try
  121. {
  122. FileStream myFile = new FileStream(_fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
  123. BinaryReader br = new BinaryReader(myFile);
  124. try
  125. {
  126. _Response.AddHeader("Accept-Ranges", "bytes");
  127. _Response.Buffer = false;
  128. long fileLength = myFile.Length;
  129. long startBytes = 0;
  130. int pack = 10240; //10K bytes
  131. int sleep = (int)Math.Floor((double)(1000 * pack / _speed)) + 1;
  132. if (_Request.Headers["Range"] != null)
  133. {
  134. _Response.StatusCode = 206;
  135. string[] range = _Request.Headers["Range"].Split(new char[] { '=', '-' });
  136. startBytes = Convert.ToInt64(range[1]);
  137. }
  138. _Response.AddHeader("Content-Length", (fileLength - startBytes).ToString());
  139. if (startBytes != 0)
  140. {
  141. _Response.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", startBytes, fileLength - 1, fileLength));
  142. }
  143. _Response.AddHeader("Connection", "Keep-Alive");
  144. _Response.ContentType = "application/octet-stream";
  145. _Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(_fileName, System.Text.Encoding.UTF8));
  146. br.BaseStream.Seek(startBytes, SeekOrigin.Begin);
  147. int maxCount = (int)Math.Floor((double)((fileLength - startBytes) / pack)) + 1;
  148. for (int i = 0; i < maxCount; i++)
  149. {
  150. if (_Response.IsClientConnected)
  151. {
  152. _Response.BinaryWrite(br.ReadBytes(pack));
  153. Thread.Sleep(sleep);
  154. }
  155. else
  156. {
  157. i = maxCount;
  158. }
  159. }
  160. }
  161. catch
  162. {
  163. return false;
  164. }
  165. finally
  166. {
  167. br.Close();
  168. myFile.Close();
  169. }
  170. }
  171. catch
  172. {
  173. return false;
  174. }
  175. return true;
  176. }
  177. }
  178. }