ControllerExtensions.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. if (stream.CanSeek)
  85. {
  86. stream.Seek(0, SeekOrigin.Begin);
  87. }
  88. return new ResumeFileStreamResult(stream, contentType, etag)
  89. {
  90. FileDownloadName = fileDownloadName
  91. };
  92. }
  93. /// <summary>
  94. /// 可断点续传和多线程下载的FileResult
  95. /// </summary>
  96. /// <param name="controller"></param>
  97. /// <param name="virtualPath">服务端本地文件的虚拟路径</param>
  98. /// <param name="contentType">Content-Type</param>
  99. /// <param name="fileDownloadName">下载的文件名</param>
  100. /// <returns></returns>
  101. public static ResumeVirtualFileResult ResumeFile(this ControllerBase controller, string virtualPath, string contentType, string fileDownloadName)
  102. {
  103. return ResumeFile(controller, virtualPath, contentType, fileDownloadName, null);
  104. }
  105. /// <summary>
  106. /// 可断点续传和多线程下载的FileResult
  107. /// </summary>
  108. /// <param name="controller"></param>
  109. /// <param name="virtualPath">服务端本地文件的虚拟路径</param>
  110. /// <param name="fileDownloadName">下载的文件名</param>
  111. /// <returns></returns>
  112. public static ResumeVirtualFileResult ResumeFile(this ControllerBase controller, string virtualPath, string fileDownloadName)
  113. {
  114. return ResumeFile(controller, virtualPath, MimeMapper.GetMimeFromPath(virtualPath), fileDownloadName, null);
  115. }
  116. /// <summary>
  117. /// 可断点续传和多线程下载的FileResult
  118. /// </summary>
  119. /// <param name="controller"></param>
  120. /// <param name="virtualPath">服务端本地文件的虚拟路径</param>
  121. /// <param name="contentType">Content-Type</param>
  122. /// <param name="fileDownloadName">下载的文件名</param>
  123. /// <param name="etag">ETag</param>
  124. /// <returns></returns>
  125. public static ResumeVirtualFileResult ResumeFile(this ControllerBase controller, string virtualPath, string contentType, string fileDownloadName, string etag)
  126. {
  127. return new ResumeVirtualFileResult(virtualPath, contentType, etag)
  128. {
  129. FileDownloadName = fileDownloadName
  130. };
  131. }
  132. /// <summary>
  133. /// 可断点续传和多线程下载的FileResult
  134. /// </summary>
  135. /// <param name="controller"></param>
  136. /// <param name="physicalPath">服务端本地文件的物理路径</param>
  137. /// <param name="contentType">Content-Type</param>
  138. /// <param name="fileDownloadName">下载的文件名</param>
  139. /// <returns></returns>
  140. public static ResumePhysicalFileResult ResumePhysicalFile(this ControllerBase controller, string physicalPath, string contentType, string fileDownloadName)
  141. {
  142. return ResumePhysicalFile(controller, physicalPath, contentType, fileDownloadName, etag: null);
  143. }
  144. /// <summary>
  145. /// 可断点续传和多线程下载的FileResult
  146. /// </summary>
  147. /// <param name="controller"></param>
  148. /// <param name="physicalPath">服务端本地文件的物理路径</param>
  149. /// <param name="fileDownloadName">下载的文件名</param>
  150. /// <returns></returns>
  151. public static ResumePhysicalFileResult ResumePhysicalFile(this ControllerBase controller, string physicalPath, string fileDownloadName)
  152. {
  153. return ResumePhysicalFile(controller, physicalPath, MimeMapper.GetMimeFromPath(physicalPath), fileDownloadName, etag: null);
  154. }
  155. /// <summary>
  156. /// 可断点续传和多线程下载的FileResult
  157. /// </summary>
  158. /// <param name="controller"></param>
  159. /// <param name="physicalPath">服务端本地文件的物理路径</param>
  160. /// <param name="contentType">Content-Type</param>
  161. /// <param name="fileDownloadName">下载的文件名</param>
  162. /// <param name="etag">ETag</param>
  163. /// <returns></returns>
  164. public static ResumePhysicalFileResult ResumePhysicalFile(this ControllerBase controller, string physicalPath, string contentType, string fileDownloadName, string etag)
  165. {
  166. return new ResumePhysicalFileResult(physicalPath, contentType, etag)
  167. {
  168. FileDownloadName = fileDownloadName
  169. };
  170. }
  171. }