ControllerExtensions.cs 7.7 KB

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