ResumeFileStreamResult.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using Microsoft.AspNetCore.Mvc;
  2. using Microsoft.AspNetCore.Mvc.Infrastructure;
  3. using Microsoft.Net.Http.Headers;
  4. namespace Masuit.Tools.AspNetCore.ResumeFileResults.ResumeFileResult;
  5. /// <summary>
  6. /// 基于Stream的ResumeFileStreamResult
  7. /// </summary>
  8. public class ResumeFileStreamResult : FileStreamResult, IResumeFileResult
  9. {
  10. /// <summary>
  11. /// 构造函数
  12. /// </summary>
  13. /// <param name="stream">文件流</param>
  14. /// <param name="contentType">Content-Type</param>
  15. /// <param name="etag">ETag</param>
  16. public ResumeFileStreamResult(Stream stream, string contentType, string etag = null) : this(stream, MediaTypeHeaderValue.Parse(contentType), !string.IsNullOrEmpty(etag) ? EntityTagHeaderValue.Parse(etag) : null)
  17. {
  18. }
  19. /// <summary>
  20. /// 构造函数
  21. /// </summary>
  22. /// <param name="stream">文件流</param>
  23. /// <param name="contentType">Content-Type</param>
  24. /// <param name="etag">ETag</param>
  25. public ResumeFileStreamResult(Stream stream, MediaTypeHeaderValue contentType, EntityTagHeaderValue etag = null) : base(stream, contentType)
  26. {
  27. if (stream.CanSeek)
  28. {
  29. stream.Seek(0, SeekOrigin.Begin);
  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<ResumeFileStreamResult>>();
  44. return executor.ExecuteAsync(context, this);
  45. }
  46. }