The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BufferedFileStreamResult.cs 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using Microsoft.AspNetCore.Http;
  2. using Microsoft.AspNetCore.Http.Features;
  3. using Microsoft.AspNetCore.Mvc;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Net.Http.Headers;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Web;
  12. namespace Teknik.Utilities
  13. {
  14. /// <summary>
  15. /// MVC action result that generates the file content using a delegate that writes the content directly to the output stream.
  16. /// </summary>
  17. public class BufferedFileStreamResult : FileResult
  18. {
  19. private readonly Func<HttpResponse, Task> responseDelegate;
  20. private readonly bool bufferOutput;
  21. public BufferedFileStreamResult(string contentType, Func<HttpResponse, Task> response, bool bufferOutput) : base (contentType)
  22. {
  23. if (response == null)
  24. throw new ArgumentNullException("content");
  25. this.responseDelegate = response;
  26. this.bufferOutput = bufferOutput;
  27. }
  28. public override Task ExecuteResultAsync(ActionContext context)
  29. {
  30. if (!bufferOutput)
  31. {
  32. var bufferingFeature = context.HttpContext.Features.Get<IHttpBufferingFeature>();
  33. bufferingFeature?.DisableResponseBuffering();
  34. }
  35. return responseDelegate(context.HttpContext.Response);
  36. }
  37. }
  38. }