Hi
This is my first post here, so hope everything makes sense.
I have the following code that works fine. I create an excel file using ClosedXML. I then use the MemoryStream to return a byte array.
I then return a FileResult specifying the type.
What I want to do is do this for multiple files and I believe the only way is by creating a zip file.
This is the code I have.
This creates a zip file with the excel files inside, but the files are corrupt. I get the following error.
It seems like the issue is that I'm not specifying what the files are like in the code where I return a single fileresult file.
Really hopeful that this can be done.
Thank you in advance
This is my first post here, so hope everything makes sense.
I have the following code that works fine. I create an excel file using ClosedXML. I then use the MemoryStream to return a byte array.
I then return a FileResult specifying the type.
C#:
var bytes = repo.DownloadRotas(code, wcDate);
return File(bytes, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "rotas.xlsx");
What I want to do is do this for multiple files and I believe the only way is by creating a zip file.
This is the code I have.
C#:
using (var ms = new MemoryStream())
{
using (var archive = new ZipArchive(ms, ZipArchiveMode.Create, true))
{
// loop through each person
foreach (var code in codes)
{
// CREATE AN ENTRY WITH THE XLSX FILENAME
var filename = string.Format("Rotas - {0}.xlsx", code);
// GET THE BYTES FOR THE FILE FROM YOUR DOWNLOADROTAS METHOD
var bytes = repo.DownloadRotas(code, wcDate);
var demoFile = archive.CreateEntry(filename);
using (var entryStream = demoFile.Open())
using (var streamWriter = new StreamWriter(entryStream))
{
// WRITE BYTES IN HERE
streamWriter.Write(bytes);
}
}
}
return File(ms.ToArray(), "application/zip", "test.zip");
}
This creates a zip file with the excel files inside, but the files are corrupt. I get the following error.
Excel cannot open the file.... becase the file format or file extension is not valid .....
It seems like the issue is that I'm not specifying what the files are like in the code where I return a single fileresult file.
Really hopeful that this can be done.
Thank you in advance