纽威
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.

158 lines
6.2 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. public static bool ResponseFile(HttpRequest _Request, HttpResponse _Response, string _fileName, string _fullPath, long _speed)
  91. {
  92. try
  93. {
  94. FileStream myFile = new FileStream(_fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
  95. BinaryReader br = new BinaryReader(myFile);
  96. try
  97. {
  98. _Response.AddHeader("Accept-Ranges", "bytes");
  99. _Response.Buffer = false;
  100. long fileLength = myFile.Length;
  101. long startBytes = 0;
  102. int pack = 10240; //10K bytes
  103. int sleep = (int)Math.Floor((double)(1000 * pack / _speed)) + 1;
  104. if (_Request.Headers["Range"] != null)
  105. {
  106. _Response.StatusCode = 206;
  107. string[] range = _Request.Headers["Range"].Split(new char[] { '=', '-' });
  108. startBytes = Convert.ToInt64(range[1]);
  109. }
  110. _Response.AddHeader("Content-Length", (fileLength - startBytes).ToString());
  111. if (startBytes != 0)
  112. {
  113. _Response.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", startBytes, fileLength - 1, fileLength));
  114. }
  115. _Response.AddHeader("Connection", "Keep-Alive");
  116. _Response.ContentType = "application/octet-stream";
  117. _Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(_fileName, System.Text.Encoding.UTF8));
  118. br.BaseStream.Seek(startBytes, SeekOrigin.Begin);
  119. int maxCount = (int)Math.Floor((double)((fileLength - startBytes) / pack)) + 1;
  120. for (int i = 0; i < maxCount; i++)
  121. {
  122. if (_Response.IsClientConnected)
  123. {
  124. _Response.BinaryWrite(br.ReadBytes(pack));
  125. Thread.Sleep(sleep);
  126. }
  127. else
  128. {
  129. i = maxCount;
  130. }
  131. }
  132. }
  133. catch
  134. {
  135. return false;
  136. }
  137. finally
  138. {
  139. br.Close();
  140. myFile.Close();
  141. }
  142. }
  143. catch
  144. {
  145. return false;
  146. }
  147. return true;
  148. }
  149. }
  150. }