The following code works, but I’m wondering if the MemoryStream created is closed properly. How should this be performed or does FileStreamResult handle it for me?
public FileStreamResult DownloadBudgetedRoleOpportunities(
Guid projectGuid,
IEnumerable<Guid> guidRequiredRoles)
{
var rolebroker = new ProjectRoleBudgetBroker();
var memstream = rolebroker.CreateBudgetedRoleOpportunies(
projectGuid,
guidRequiredRoles);
var fsr = new FileStreamResult ( memstream, "application/csv" )
{
FileDownloadName = "RoleOpportunities.csv"
};
// memstream.Close(); throws exception
return fsr;
}
The
FileStreamResultwill do that for you. When in doubt always check the code, because the code never lies and since ASP.NET MVC is open source it’s even more easy to view the code.A quick search on Google for FileStreamResult.cs lets you verify that in the
WriteFilemethod the stream is correctly disposed using theusingstatement. (no pun intended)