Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a MemoryStream with FileStreamResult possible? [duplicate]

I'm using DotNetZip to create a zip file and pass it to a FileResult. On debug, I can verify that the MemoryStream contains a file, but when I run it through FileStreamResult, it returns 0bytes:

public FileResult GetZipFiles(int documentId) {
       var file = fileRepository.Get(documentId);
       var zip = new ZipFile();
       var stream = new MemoryStream();

       var filePath = Path.Combine(UploadsFolder, Path.GetFileName(file.Id));

       zip.AddFile(filePath);
       zip.Save(stream);

       var result = new FileStreamResult(stream, "application/zip") 
                    { FileDownloadName = "hey.zip" };

       return result;
 }

Again, I can verify that stream is not empty, but this will always return the file hey.zip as 0bytes. I must be using MemoryStream wrong here? Or FileStreamResult does something I'm not expecting it to do? I've used FileStreamResult before, but not with MemoryStream.

like image 761
chum of chance Avatar asked Sep 13 '10 16:09

chum of chance


1 Answers

Have you tried setting stream.Position = 0; after you do the zip.Save(stream)?

Also, you might confirm that data is actually being written to the stream. Check stream.Length after zip.Save. If stream.Length is zero, then nothing's being written.

like image 133
Jim Mischel Avatar answered Sep 30 '22 17:09

Jim Mischel