ControllerExtensions.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. using Masuit.Tools.Mime;
  2. using Masuit.Tools.AspNetCore.ResumeFileResults.ResumeFileResult;
  3. using Microsoft.AspNetCore.Mvc;
  4. using System.IO;
  5. namespace Masuit.Tools.AspNetCore.ResumeFileResults.Extensions
  6. {
  7. /// <summary>
  8. /// Controller扩展方法
  9. /// </summary>
  10. public static class ControllerExtensions
  11. {
  12. private static readonly IMimeMapper _mimeMapper = new MimeMapper();
  13. /// <summary>
  14. /// 可断点续传和多线程下载的FileResult
  15. /// </summary>
  16. /// <param name="controller"></param>
  17. /// <param name="fileContents">文件二进制流</param>
  18. /// <param name="contentType">Content-Type</param>
  19. /// <param name="fileDownloadName">下载的文件名</param>
  20. /// <returns></returns>
  21. public static ResumeFileContentResult ResumeFile(this ControllerBase controller, byte[] fileContents, string contentType, string fileDownloadName)
  22. {
  23. return ResumeFile(controller, fileContents, contentType, fileDownloadName, null);
  24. }
  25. /// <summary>
  26. /// 可断点续传和多线程下载的FileResult
  27. /// </summary>
  28. /// <param name="controller"></param>
  29. /// <param name="fileContents">文件二进制流</param>
  30. /// <param name="fileDownloadName">下载的文件名</param>
  31. /// <returns></returns>
  32. public static ResumeFileContentResult ResumeFile(this ControllerBase controller, byte[] fileContents, string fileDownloadName)
  33. {
  34. return ResumeFile(controller, fileContents, _mimeMapper.GetMimeFromPath(fileDownloadName), fileDownloadName, null);
  35. }
  36. /// <summary>
  37. /// 可断点续传和多线程下载的FileResult
  38. /// </summary>
  39. /// <param name="controller"></param>
  40. /// <param name="fileContents">文件二进制流</param>
  41. /// <param name="contentType">Content-Type</param>
  42. /// <param name="fileDownloadName">下载的文件名</param>
  43. /// <param name="etag">ETag</param>
  44. /// <returns></returns>
  45. public static ResumeFileContentResult ResumeFile(this ControllerBase controller, byte[] fileContents, string contentType, string fileDownloadName, string etag)
  46. {
  47. return new ResumeFileContentResult(fileContents, contentType, etag)
  48. {
  49. FileDownloadName = fileDownloadName
  50. };
  51. }
  52. /// <summary>
  53. /// 可断点续传和多线程下载的FileResult
  54. /// </summary>
  55. /// <param name="controller"></param>
  56. /// <param name="fileStream">文件二进制流</param>
  57. /// <param name="contentType">Content-Type</param>
  58. /// <param name="fileDownloadName">下载的文件名</param>
  59. /// <returns></returns>
  60. public static ResumeFileStreamResult ResumeFile(this ControllerBase controller, FileStream fileStream, string contentType, string fileDownloadName)
  61. {
  62. return ResumeFile(controller, fileStream, contentType, fileDownloadName, null);
  63. }
  64. /// <summary>
  65. /// 可断点续传和多线程下载的FileResult
  66. /// </summary>
  67. /// <param name="controller"></param>
  68. /// <param name="fileStream">文件二进制流</param>
  69. /// <param name="fileDownloadName">下载的文件名</param>
  70. /// <returns></returns>
  71. public static ResumeFileStreamResult ResumeFile(this ControllerBase controller, FileStream fileStream, string fileDownloadName)
  72. {
  73. return ResumeFile(controller, fileStream, _mimeMapper.GetMimeFromPath(fileDownloadName), fileDownloadName, null);
  74. }
  75. /// <summary>
  76. /// 可断点续传和多线程下载的FileResult
  77. /// </summary>
  78. /// <param name="controller"></param>
  79. /// <param name="fileStream">文件二进制流</param>
  80. /// <param name="contentType">Content-Type</param>
  81. /// <param name="fileDownloadName">下载的文件名</param>
  82. /// <param name="etag">ETag</param>
  83. /// <returns></returns>
  84. public static ResumeFileStreamResult ResumeFile(this ControllerBase controller, FileStream fileStream, string contentType, string fileDownloadName, string etag)
  85. {
  86. return new ResumeFileStreamResult(fileStream, contentType, etag)
  87. {
  88. FileDownloadName = fileDownloadName
  89. };
  90. }
  91. /// <summary>
  92. /// 可断点续传和多线程下载的FileResult
  93. /// </summary>
  94. /// <param name="controller"></param>
  95. /// <param name="virtualPath">服务端本地文件的虚拟路径</param>
  96. /// <param name="contentType">Content-Type</param>
  97. /// <param name="fileDownloadName">下载的文件名</param>
  98. /// <returns></returns>
  99. public static ResumeVirtualFileResult ResumeFile(this ControllerBase controller, string virtualPath, string contentType, string fileDownloadName)
  100. {
  101. return ResumeFile(controller, virtualPath, contentType, fileDownloadName, null);
  102. }
  103. /// <summary>
  104. /// 可断点续传和多线程下载的FileResult
  105. /// </summary>
  106. /// <param name="controller"></param>
  107. /// <param name="virtualPath">服务端本地文件的虚拟路径</param>
  108. /// <param name="fileDownloadName">下载的文件名</param>
  109. /// <returns></returns>
  110. public static ResumeVirtualFileResult ResumeFile(this ControllerBase controller, string virtualPath, string fileDownloadName)
  111. {
  112. return ResumeFile(controller, virtualPath, _mimeMapper.GetMimeFromPath(virtualPath), fileDownloadName, null);
  113. }
  114. /// <summary>
  115. /// 可断点续传和多线程下载的FileResult
  116. /// </summary>
  117. /// <param name="controller"></param>
  118. /// <param name="virtualPath">服务端本地文件的虚拟路径</param>
  119. /// <param name="contentType">Content-Type</param>
  120. /// <param name="fileDownloadName">下载的文件名</param>
  121. /// <param name="etag">ETag</param>
  122. /// <returns></returns>
  123. public static ResumeVirtualFileResult ResumeFile(this ControllerBase controller, string virtualPath, string contentType, string fileDownloadName, string etag)
  124. {
  125. return new ResumeVirtualFileResult(virtualPath, contentType, etag)
  126. {
  127. FileDownloadName = fileDownloadName
  128. };
  129. }
  130. /// <summary>
  131. /// 可断点续传和多线程下载的FileResult
  132. /// </summary>
  133. /// <param name="controller"></param>
  134. /// <param name="physicalPath">服务端本地文件的物理路径</param>
  135. /// <param name="contentType">Content-Type</param>
  136. /// <param name="fileDownloadName">下载的文件名</param>
  137. /// <returns></returns>
  138. public static ResumePhysicalFileResult ResumePhysicalFile(this ControllerBase controller, string physicalPath, string contentType, string fileDownloadName)
  139. {
  140. return ResumePhysicalFile(controller, physicalPath, contentType, fileDownloadName, etag: null);
  141. }
  142. /// <summary>
  143. /// 可断点续传和多线程下载的FileResult
  144. /// </summary>
  145. /// <param name="controller"></param>
  146. /// <param name="physicalPath">服务端本地文件的物理路径</param>
  147. /// <param name="fileDownloadName">下载的文件名</param>
  148. /// <returns></returns>
  149. public static ResumePhysicalFileResult ResumePhysicalFile(this ControllerBase controller, string physicalPath, string fileDownloadName)
  150. {
  151. return ResumePhysicalFile(controller, physicalPath, _mimeMapper.GetMimeFromPath(physicalPath), fileDownloadName, etag: null);
  152. }
  153. /// <summary>
  154. /// 可断点续传和多线程下载的FileResult
  155. /// </summary>
  156. /// <param name="controller"></param>
  157. /// <param name="physicalPath">服务端本地文件的物理路径</param>
  158. /// <param name="contentType">Content-Type</param>
  159. /// <param name="fileDownloadName">下载的文件名</param>
  160. /// <param name="etag">ETag</param>
  161. /// <returns></returns>
  162. public static ResumePhysicalFileResult ResumePhysicalFile(this ControllerBase controller, string physicalPath, string contentType, string fileDownloadName, string etag)
  163. {
  164. return new ResumePhysicalFileResult(physicalPath, contentType, etag)
  165. {
  166. FileDownloadName = fileDownloadName
  167. };
  168. }
  169. }
  170. }