Search code examples
asp.net-corechunked-encodingkestrel-http-serverfileresult

Prevent FileResult chunked response


I have an ASP.NET Core 1.2 application which sends a file to a user. I use a FileResult for this purpose. My code looks like:

return PhysicalFile(Path.GetFullPath(filePath), QMimeTypeMap.GetMimeType(Path.GetExtension(file.Name)));

I have tried using response buffering as in https://github.com/aspnet/BasicMiddleware/blob/dev/samples/ResponseBufferingSample/Startup.cs#L17, but it did not work for me.

This returns the file correctly, but uses Transfer-Encoding: Chunked, which causes downloads to appear in "indeterminate" form i.e. without a progress bar. I have tried setting the Content-Length header myself, but it is removed automatically.

EDIT: Please note that as mentioned above, I have already tried response buffering. This did not solve my issue.

EDIT 2: The following is an SSCCE of the code I use

FilesController.cs > Action

[HttpGet("files")]
public async Task<IActionResult> Download(string driveName, [Bind(Prefix = "id")] uint? fileId = null, [Bind(Prefix = "download")] int forceDownload = 0)
{
    // Send file info
    string filePath = "...";
    HttpContext.Response.Headers["Content-Length"] = new System.IO.FileInfo(filePath).Length.ToString();
    return PhysicalFile(Path.GetFullPath(filePath), QMimeTypeMap.GetMimeType(Path.GetExtension(filePath)));
}

Startup.cs > Configure

app.UseStaticFiles();
app.UseResponseBuffering();
app.UseMvc();

Solution

  • This seems to have been a bug in the BrowserLink middleware. It was patched by MS a while back. Read the full thread at https://github.com/aspnet/Mvc/issues/5999 .