0

I have a very weird behaviour in a legacy repository that I cloned and try to work on.

There is an action for downloading files (files are hosted on AWS): (redundant code omitted for clarity).

public virtual async Task<ActionResult> Entity(EntityModel entity)
        {
               using (Stream stream = downloadFileObject(model.Updates[GetUpdateNumberFromQueryString()].UpdateLink))
                using (MemoryStream ms = new MemoryStream())
                {
                    await stream.CopyToAsync(ms).ConfigureAwait(false);
                    return File(ms.ToArray(), "application/octet-stream", "The file.exe");
                }
        }

The DownloadFileObject method connects to AWS, creates a request and returns the response stream:

public Stream GetS3FileObject(string objectKey, string fileName)
    {
        GetObjectRequest request = new GetObjectRequest
        {
            BucketName = bucketName,
            Key = objectKey,
        };
        GetObjectResponse response = s3Client.GetObject(request);
        return response.ResponseStream;
    }

This returns a proper stream with no errors, I can see that the lenght indicates something around 35 mb, which is the proper size of the file I am getting.

Now, I tried several approaches for returning this stream as downloadable file, and they all result in something very weird.

Instead of getting the file, the browser retrieves a 110kb text file which pretty much looks like my current view html...

The returned HTML contains the following error message (apart from the regular site content)

Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'. OutputStream is not available when a custom TextWriter is used.

I tried several approaches, where the first and most obvious was just:

var stream = DownloadFileObject(model.Updates[GetUpdateNumberFromQueryString()].UpdateLink);
return File(stream, "application/octet-stream", "The file.exe");

Then I tried to copy that stream to a byte array and return that - however, in all cases the result is the same. Up to the last moment, the debugger shows that my stream/byte array is 35mb large.

Here are the headers:

enter image description here

enter image description here

Also, the rendered Download button html is as below:

<a href="/support/product-support/software/TheProductName" class="btn btn-default">Download update</a>

And the button is being created in the view without any HTML helpers (Action.Link() etc)

<a href="@updateVersions.Link" class="btn btn-default">@Model.SiteLabelDownloadUpdate</a>

Any ideas what can be going on?

Bartosz
  • 4,406
  • 7
  • 41
  • 80

0 Answers0