Skip to content

Instantly share code, notes, and snippets.

@hazzik
Created July 3, 2012 10:44
Show Gist options
  • Save hazzik/3038986 to your computer and use it in GitHub Desktop.
Save hazzik/3038986 to your computer and use it in GitHub Desktop.
public class DelegatedFileStreamResult : FileResult
{
readonly Action<Stream> _writer;
public DelegatedFileStreamResult(Action<Stream> writer, string contentType)
: base(contentType)
{
_writer = writer;
}
protected override void WriteFile(HttpResponseBase response)
{
_writer(response.OutputStream);
}
}
@kazimanzurrashid
Copy link

// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.

using System.IO;

namespace System.Web.Mvc
{
public class FileStreamResult : FileResult
{
// default buffer size as defined in BufferedStream type
private const int BufferSize = 0x1000;

    public FileStreamResult(Stream fileStream, string contentType)
        : base(contentType)
    {
        if (fileStream == null)
        {
            throw new ArgumentNullException("fileStream");
        }

        FileStream = fileStream;
    }

    public Stream FileStream { get; private set; }

    protected override void WriteFile(HttpResponseBase response)
    {
        // grab chunks of data and write to the output stream
        Stream outputStream = response.OutputStream;
        using (FileStream)
        {
            byte[] buffer = new byte[BufferSize];

            while (true)
            {
                int bytesRead = FileStream.Read(buffer, 0, BufferSize);
                if (bytesRead == 0)
                {
                    // no more data
                    break;
                }

                outputStream.Write(buffer, 0, bytesRead);
            }
        }
    }
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment