ResumeFileContentResult.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using Microsoft.AspNetCore.Mvc;
  2. using Microsoft.AspNetCore.Mvc.Infrastructure;
  3. using Microsoft.Extensions.DependencyInjection;
  4. using Microsoft.Net.Http.Headers;
  5. using System;
  6. using System.Threading.Tasks;
  7. namespace Masuit.Tools.AspNetCore.ResumeFileResults.ResumeFileResult
  8. {
  9. /// <summary>
  10. /// 基于Stream的ResumeFileContentResult
  11. /// </summary>
  12. public class ResumeFileContentResult : FileContentResult, IResumeFileResult
  13. {
  14. /// <summary>
  15. /// 构造函数
  16. /// </summary>
  17. /// <param name="fileContents">文件二进制流</param>
  18. /// <param name="contentType">Content-Type</param>
  19. /// <param name="etag">ETag</param>
  20. public ResumeFileContentResult(byte[] fileContents, string contentType, string etag = null) : this(fileContents, MediaTypeHeaderValue.Parse(contentType), !string.IsNullOrEmpty(etag) ? EntityTagHeaderValue.Parse(etag) : null)
  21. {
  22. }
  23. /// <summary>
  24. /// 构造函数
  25. /// </summary>
  26. /// <param name="fileContents">文件二进制流</param>
  27. /// <param name="contentType">Content-Type</param>
  28. /// <param name="etag">ETag</param>
  29. public ResumeFileContentResult(byte[] fileContents, MediaTypeHeaderValue contentType, EntityTagHeaderValue etag = null) : base(fileContents, contentType)
  30. {
  31. EntityTag = etag;
  32. EnableRangeProcessing = true;
  33. }
  34. /// <inheritdoc/>
  35. public string FileInlineName { get; set; }
  36. /// <inheritdoc/>
  37. public override Task ExecuteResultAsync(ActionContext context)
  38. {
  39. if (context == null)
  40. {
  41. throw new ArgumentNullException(nameof(context));
  42. }
  43. var executor = context.HttpContext.RequestServices.GetRequiredService<IActionResultExecutor<ResumeFileContentResult>>();
  44. return executor.ExecuteAsync(context, this);
  45. }
  46. }
  47. }