public ActionResult GetFile(string dateStr, string serverName, string foodName)
{
using (var memoStream = new MemoryStream(1024 * 5))
{
using (StreamWriter writer = new StreamWriter(memoStream))
{
var dataFilter = new CapacityDataFilter(dateStr, serverName, feedName);
dataFilter.FilterDataByServerAndFeed();
writer.WriteLine("Feed, StreamMin, TotalMsgNumber, TotalMsgSize, PeakRateMsgNumber, PeakRateMsgSize");
foreach (var element in dataFilter.DataInTheDay)
{
writer.WriteLine(string.Format("{0},{1},{2},{3},{4},{5}",
element.Feed, element.StreamMin,
element.TotalMsgNumber, element.TotalMsgSize,
element.PeakRateMsgNumber, element.PeakRateMsgSize));
}
return File(memoStream, "text/csv", fileName);
}
}
}
Exception Details: System.ObjectDisposedException: Cannot access a closed Stream.
this action doesn’t work, how to make a download action?
“this action not work” is wrong way to explain problems…
But in this case you are lucky: you are trying to send incomplete data in memory stream that is seeked to the end, and as second issue stream will be disposed by the time File action actually executes.
The best fix is to move code outisde inner
usingand return new MemoryStream on buffer of old stream:Flush + Seek as proposed by armen.shimoon would work too in other cases when you are using stream immediately: