I have this Azure function, that should return a zip file.
On returning the FileStreamResult i get
System.Private.CoreLib: Cannot access a closed Stream.
But my return statement is within the using of the memoryStream.
Any help is very appreciated.
BR Kresten
namespace returnZipfile
{
public static class ReturnZipfile
{
[FunctionName("ReturnZipfile")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
using (var memoryStream = new MemoryStream())
{
using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
{
ZipArchiveEntry archiveFile = archive.CreateEntry("Manifest.xml");
using (var entryStream = archiveFile.Open())
{
using (var streamWriter = new StreamWriter(entryStream))
{
streamWriter.Write("Manifest content");
}
}
archiveFile = archive.CreateEntry("PackageHeader.xml");
using (var entryStream = archiveFile.Open())
{
using (var streamWriter = new StreamWriter(entryStream))
{
streamWriter.Write("PackageHeader content");
}
}
archiveFile = archive.CreateEntry("VendorInvoiceHeaders.csv");
using (var entryStream = archiveFile.Open())
{
using (var streamWriter = new StreamWriter(entryStream))
{
streamWriter.Write("Header content");
}
}
}
return new FileStreamResult(memoryStream, "application/octet-stream")
{
FileDownloadName = "datapackage.zip"
};
}
}
}
}