ControllerExtensions.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using Masuit.Tools.AspNetCore.ResumeFileResults.ResumeFileResult;
  2. using Microsoft.AspNetCore.Mvc;
  3. using System.IO;
  4. namespace Masuit.Tools.AspNetCore.ResumeFileResults.Extensions
  5. {
  6. /// <summary>
  7. /// Controller扩展方法
  8. /// </summary>
  9. public static class ControllerExtensions
  10. {
  11. /// <summary>
  12. /// 可断点续传和多线程下载的FileResult
  13. /// </summary>
  14. /// <param name="controller"></param>
  15. /// <param name="fileContents">文件二进制流</param>
  16. /// <param name="contentType">Content-Type</param>
  17. /// <returns></returns>
  18. public static ResumeFileContentResult ResumeFile(this ControllerBase controller, byte[] fileContents, string contentType)
  19. {
  20. return ResumeFile(controller, fileContents, contentType, fileDownloadName: null);
  21. }
  22. /// <summary>
  23. /// 可断点续传和多线程下载的FileResult
  24. /// </summary>
  25. /// <param name="controller"></param>
  26. /// <param name="fileContents">文件二进制流</param>
  27. /// <param name="contentType">Content-Type</param>
  28. /// <param name="fileDownloadName">下载的文件名</param>
  29. /// <returns></returns>
  30. public static ResumeFileContentResult ResumeFile(this ControllerBase controller, byte[] fileContents, string contentType, string fileDownloadName)
  31. {
  32. return ResumeFile(controller, fileContents, contentType, fileDownloadName, etag: 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="fileStream">文件二进制流</param>
  55. /// <param name="contentType">Content-Type</param>
  56. /// <returns></returns>
  57. public static ResumeFileStreamResult ResumeFile(this ControllerBase controller, Stream fileStream, string contentType)
  58. {
  59. return ResumeFile(controller, fileStream, contentType, fileDownloadName: null);
  60. }
  61. /// <summary>
  62. /// 可断点续传和多线程下载的FileResult
  63. /// </summary>
  64. /// <param name="controller"></param>
  65. /// <param name="fileStream">文件二进制流</param>
  66. /// <param name="contentType">Content-Type</param>
  67. /// <param name="fileDownloadName">下载的文件名</param>
  68. /// <returns></returns>
  69. public static ResumeFileStreamResult ResumeFile(this ControllerBase controller, Stream fileStream, string contentType, string fileDownloadName)
  70. {
  71. return ResumeFile(controller, fileStream, contentType, fileDownloadName, etag: null);
  72. }
  73. /// <summary>
  74. /// 可断点续传和多线程下载的FileResult
  75. /// </summary>
  76. /// <param name="controller"></param>
  77. /// <param name="fileStream">文件二进制流</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 fileStream, string contentType, string fileDownloadName, string etag)
  83. {
  84. return new ResumeFileStreamResult(fileStream, 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. /// <returns></returns>
  96. public static ResumeVirtualFileResult ResumeFile(this ControllerBase controller, string virtualPath, string contentType)
  97. {
  98. return ResumeFile(controller, virtualPath, contentType, fileDownloadName: null);
  99. }
  100. /// <summary>
  101. /// 可断点续传和多线程下载的FileResult
  102. /// </summary>
  103. /// <param name="controller"></param>
  104. /// <param name="virtualPath">服务端本地文件的虚拟路径</param>
  105. /// <param name="contentType">Content-Type</param>
  106. /// <param name="fileDownloadName">下载的文件名</param>
  107. /// <returns></returns>
  108. public static ResumeVirtualFileResult ResumeFile(this ControllerBase controller, string virtualPath, string contentType, string fileDownloadName)
  109. {
  110. return ResumeFile(controller, virtualPath, contentType, fileDownloadName, etag: 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. /// <returns></returns>
  135. public static ResumePhysicalFileResult ResumePhysicalFile(this ControllerBase controller, string physicalPath, string contentType)
  136. {
  137. return ResumePhysicalFile(controller, physicalPath, contentType, fileDownloadName: null, etag: null);
  138. }
  139. /// <summary>
  140. /// 可断点续传和多线程下载的FileResult
  141. /// </summary>
  142. /// <param name="controller"></param>
  143. /// <param name="physicalPath">服务端本地文件的物理路径</param>
  144. /// <param name="contentType">Content-Type</param>
  145. /// <param name="fileDownloadName">下载的文件名</param>
  146. /// <returns></returns>
  147. public static ResumePhysicalFileResult ResumePhysicalFile(this ControllerBase controller, string physicalPath, string contentType, string fileDownloadName)
  148. {
  149. return ResumePhysicalFile(controller, physicalPath, contentType, 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. }
  168. }