Example code for ASP.NET Core file response in C#

If you prefer to use your own stream wrapper, you can intercept the raw data. Alternatively, the code provided in the question disposes of both the stream and stream reader quickly. It’s worth noting that the team behind the source code of the application always copies the wrapper stream to the original body stream at the end, rather than replacing it outright (unless they’re testing with a mocked stream).


To stream a file to the client, you may opt to utilize the FileResult as your return type.

public FileResult DownloadFile(string id) {
    var content = new FileStream(myLocation,FileMode.Open, FileAccess.Read, FileShare.Read);
    var response = File(content, "application/octet-stream");//FileStreamResult
    return response;

The ASP.NET Core ActionResult documentation provides information about FileResult, which serves as the base class for various file-related action results, including FileContentResult, FileStreamResult, VirtualFileResult, and PhysicalFileResult.

Frequently Asked Questions

Posted in Uncategorized