I have very simple example to export some data to Excel file, in some reason I see the bytes in the response but no file downloaded why?
public async Task<IActionResult> exportRecordsToExcel()
{
var file = await ServiceRequestBL.ExportFO_SrviceRequestToExcel();
return file;
}
public async Task<FileStreamResult> ExportFO_SrviceRequestToExcel()
{
DataTable dt = new DataTable("Grid");
dt.Columns.AddRange(new DataColumn[13] {
new DataColumn("1"), new DataColumn("2"), new DataColumn("g"), new DataColumn("j"), new DataColumn("k"), new DataColumn("l"), new DataColumn("x"), new DataColumn("m"), new DataColumn("9"), new DataColumn("8"), new DataColumn("7"), new DataColumn("6"), new DataColumn("5")});
byte[] data = null;
using (MemoryStream stream = new MemoryStream())
{
IFormatter bf = new BinaryFormatter();
dt.RemotingFormat = SerializationFormat.Binary;
bf.Serialize(stream, dt);
data = stream.ToArray();
}
var memoryStream = new MemoryStream();
memoryStream.Seek(0, SeekOrigin.Begin);
string filename = "Report.xlsx";
return new FileStreamResult(memoryStream, "application/ms-excel") { FileDownloadName = filename };
}
2
Answers
You do not need a
using
block when usingFileStreamResult
, as this wrapper will take care of disposing the stream when it is no longer needed (it already usesusing
internally).Simply serialize your data into a stream with
Serialize(stream, ...)
as you have, and pass that stream over toFileStreamResult
. Let it take of care of the rest.You are passing an empty
MemoryStream
rather than the one you stored the data inThe stream does not need a
using
becauseFileStreamResult
will dispose it, and in any case it’s aMemoryStream
which is backed only by an array.Also, in this case there is no need for
async
as you are not awaiting anything.I note that
BinaryFormatter
is deprecated and has security issues. Consider using another serializer if possible.