The next generation of the Teknik Services. Written in ASP.NET.
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.

FileGeneratingResult.cs 1.8KB

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