using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Web; using System.Web.Mvc; namespace Teknik.Utilities { /// /// MVC action result that generates the file content using a delegate that writes the content directly to the output stream. /// public class FileGenerateResult : FileResult { private readonly Action responseDelegate; private readonly bool bufferOutput; /// /// Initializes a new instance of the class. /// /// Name of the file. /// Type of the content. /// Delegate with Stream parameter. This is the stream to which content should be written. /// use output buffering. Set to false for large files to prevent OutOfMemoryException. public FileGenerateResult(string fileName, string contentType, Action response, bool bufferOutput) : base(contentType) { if (response == null) throw new ArgumentNullException("content"); this.responseDelegate = response; this.bufferOutput = bufferOutput; } /// /// Writes the file to the response. /// /// The response object. protected override void WriteFile(System.Web.HttpResponseBase response) { response.Buffer = bufferOutput; response.BufferOutput = bufferOutput; responseDelegate(response); } } }