1

In my ASP.NET application I have a rather long running action that returns FileStreamResult. The data for the result is being constructed on the fly - it's not something cached. I expected that the "download" would start immediately but it looks like the whole stream must be emitted first.

I see this because I created a "readonly" descendant of class Stream that has CanRead returning true, all other CanX properties returning false and all other stuff except Read() throwing NotImplementedException. The rest is below:

public class TestStream : System.IO.Stream {

    // other stuff omitted
    public override int Read(byte[] buffer, int offset, int count)
    {
       callCount ++;
       if( callCount > 25 ) {
          return 0;
       }
       System.Threading.Thread.Sleep( 1000 );
       return count;
    }

    int callCount = 0;
}

This stream is used like this:

ActionResult Blah()
{
    var stream = new TestStream();
    return File( stream, "application/octet-stream" );
}

and I expect that when the URL is requested in the browser it will immediately start downloading and then advance in rather non-smooth manner until all the data is downloaded.

Instead it hangs for about 25 seconds and all the data arrives.

What am I doing wrong? Do I need the stuff for streaming partial content described in this answer or can it be done simpler?

Community
  • 1
  • 1
sharptooth
  • 167,383
  • 100
  • 513
  • 979

1 Answers1

0

Just a quick comment, you're returning a File from your Action Result, shouldn't you return a FileStreamResult instead? I assume returning a File forces your client to query for the ContentLength prior to downloading, which will not be available until the Read call returns something that is not 0

SilentDoc
  • 357
  • 2
  • 8