M
Madaxe2
Guest
I'm prototyping a method that will take in a series of JSON models, create each json file and embed them in a zip file, which will be returned back as a memory stream for the controller. Which will then download the zip file to the client.
The problem im having is that it thinks the stream is closed, and i don't know why.
Can anybody help? (If i Move the ReturnMemoryStream.Position = 0; up one level into the using block it does not fail)
Fails at : ReturnMemoryStream.Position = 0;
Error : Cannot access a closed Stream.
Second Issue if i test the code with this method in the controller it does not download anything, and i get
An unhandled exception occurred while processing the request.
ObjectDisposedException: Cannot access a closed Stream.
System.IO.MemoryStream.Read(byte[] buffer, int offset, int count)
[HttpGet("v1/ReaJetPocTestZips")]
public IActionResult ReaJetPocTestZips()
{
try
{
var result = this._IReaJetPOCImpl.GetReaJetXMLZips();
return File(result, "application/zip", "Test.zip");
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
public MemoryStream GetReaJetXMLZips(IEnumerable<JsonRequestModel> JsonRequestModels)
{
MemoryStream ReturnMemoryStream = new MemoryStream();
//compressedFileStream.Seek(0, SeekOrigin.Begin);
using (var zipArchive = new ZipArchive(ReturnMemoryStream, ZipArchiveMode.Create, false))
{
//Create a zip entry for each attachment
var zipEntry = zipArchive.CreateEntry("NewEntry");
//Get the stream of the attachment
using (var originalFileStream = GenerateStreamFromString("Marcs Stuff"))
{
using (var zipEntryStream = zipEntry.Open())
{
//Copy the attachment stream to the zip entry stream
originalFileStream.CopyTo(zipEntryStream);
}
}
}
ReturnMemoryStream.Position = 0;
return ReturnMemoryStream;
}
public MemoryStream GenerateStreamFromString(string s)
{
MemoryStream ReturnMemoryStream = new MemoryStream();
var writer = new StreamWriter(ReturnMemoryStream);
writer.Write(s);
writer.Flush();
ReturnMemoryStream.Position = 0;
return ReturnMemoryStream;
}
as busy as a bricky in beirut
Continue reading...
The problem im having is that it thinks the stream is closed, and i don't know why.
Can anybody help? (If i Move the ReturnMemoryStream.Position = 0; up one level into the using block it does not fail)
Fails at : ReturnMemoryStream.Position = 0;
Error : Cannot access a closed Stream.
Second Issue if i test the code with this method in the controller it does not download anything, and i get
An unhandled exception occurred while processing the request.
ObjectDisposedException: Cannot access a closed Stream.
System.IO.MemoryStream.Read(byte[] buffer, int offset, int count)
[HttpGet("v1/ReaJetPocTestZips")]
public IActionResult ReaJetPocTestZips()
{
try
{
var result = this._IReaJetPOCImpl.GetReaJetXMLZips();
return File(result, "application/zip", "Test.zip");
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
public MemoryStream GetReaJetXMLZips(IEnumerable<JsonRequestModel> JsonRequestModels)
{
MemoryStream ReturnMemoryStream = new MemoryStream();
//compressedFileStream.Seek(0, SeekOrigin.Begin);
using (var zipArchive = new ZipArchive(ReturnMemoryStream, ZipArchiveMode.Create, false))
{
//Create a zip entry for each attachment
var zipEntry = zipArchive.CreateEntry("NewEntry");
//Get the stream of the attachment
using (var originalFileStream = GenerateStreamFromString("Marcs Stuff"))
{
using (var zipEntryStream = zipEntry.Open())
{
//Copy the attachment stream to the zip entry stream
originalFileStream.CopyTo(zipEntryStream);
}
}
}
ReturnMemoryStream.Position = 0;
return ReturnMemoryStream;
}
public MemoryStream GenerateStreamFromString(string s)
{
MemoryStream ReturnMemoryStream = new MemoryStream();
var writer = new StreamWriter(ReturnMemoryStream);
writer.Write(s);
writer.Flush();
ReturnMemoryStream.Position = 0;
return ReturnMemoryStream;
}
as busy as a bricky in beirut
Continue reading...