Reputation: 16199
I currently use ionic to zip a number of photos and then send them to the user on a browser. Essentially something like this.
using (ZipFile zipFile = new ZipFile())
{
byte[] data = client.DownloadData("photo.png");
zipFile.AddEntry("photo.png", data);
Response.ClearContent();
Response.ClearHeaders();
Response.AppendHeader("content-disposition", "attachment; filename=Media.zip");
zipFile.Save(Response.OutputStream);
}
However sometimes there are many photo's going up to 15 mb's and there are many of them.
As such memory gets completely used and it runs out before it can finish creating the zip file. Is there a way I can accomplish zipping files and sending them to a user without using such vast amounts of memory? Maybe a different approach entirely?
Upvotes: 1
Views: 2040
Reputation: 100610
In addition to Jon's suggestion on moving everything from memory and switching to streaming.
Since you are talking about already compressed images even more extreme approach can be taken: you can create Zip with non-compressed files on the fly directly from files and immediately stream results to browser without intermediate creation of Zip in memory/on disk. I don't know if existing libraries will allow you to do so or you'll need to write your own based on the specification (see Wikipedia on Zip for links).
Side note: it is unclear from your sample if you need to store data between requests, but if you do consider some out-of-process storage for files and not session state. If you handle large amount of data - make sure to use x64 process for IIS if possible.
Upvotes: 1
Reputation: 1503280
It sounds to me like you'd be better off writing the file to disk (or a database) somewhere, ideally in a streaming fashion... then give the client a link which allows the file to be downloaded - again, in a streaming fashion. Basically, try to avoid ever requiring the complete file to be in memory at any time.
Upvotes: 5