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.

FileGenerateResult.cs 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Web;
  8. using System.Web.Mvc;
  9. namespace Teknik.Utilities
  10. {
  11. /// <summary>
  12. /// MVC action result that generates the file content using a delegate that writes the content directly to the output stream.
  13. /// </summary>
  14. public class FileGenerateResult : FileResult
  15. {
  16. private readonly Action<HttpResponseBase> responseDelegate;
  17. private readonly bool bufferOutput;
  18. /// <summary>
  19. /// Initializes a new instance of the <see cref="FileGeneratingResult" /> class.
  20. /// </summary>
  21. /// <param name="fileName">Name of the file.</param>
  22. /// <param name="contentType">Type of the content.</param>
  23. /// <param name="content">Delegate with Stream parameter. This is the stream to which content should be written.</param>
  24. /// <param name="bufferOutput">use output buffering. Set to false for large files to prevent OutOfMemoryException.</param>
  25. public FileGenerateResult(string fileName, string contentType, Action<HttpResponseBase> response, bool bufferOutput)
  26. : base(contentType)
  27. {
  28. if (response == null)
  29. throw new ArgumentNullException("content");
  30. this.responseDelegate = response;
  31. this.bufferOutput = bufferOutput;
  32. }
  33. /// <summary>
  34. /// Writes the file to the response.
  35. /// </summary>
  36. /// <param name="response">The response object.</param>
  37. protected override void WriteFile(System.Web.HttpResponseBase response)
  38. {
  39. response.Buffer = bufferOutput;
  40. response.BufferOutput = bufferOutput;
  41. responseDelegate(response);
  42. }
  43. }
  44. }