c# - Most efficient way to write large files to HttpResponse in ASP.NET -
i creating zip file on fly. using dotnetzip doing this. size of zip file can go 500 mb, many users trying download @ same time.
what efficient way in can serve files? if given choice, rather not save files on disk either, since might cause severe disk space constraints.
edit:more use case:
we hosting our files in sharepoint 2010 asset library hosted in intranet site has users around world. files typically range 10-80 mbs. users ability download multiple files @ once.
well, in theory:
for traditional asp.net application should able write response data (bytes) httpcontext.response.outputstream
(how http response context depend on how handle download request e.g. if implementing ihttphandler
http context passed you).
looking @ dotnetzip examples looks save
method takes stream, simple as
zip.save(context.response.outputstream);
if zip file re-used , downloaded many users can instead write zip memorystream
when creating zip allows later copy contents of memory stream individual responses:
memorystream stream = new memorystream() zip.save(stream); // save data somewhere common (e.g. cache it) byte[] data = stream.toarray();
to write data response:
memorystream reader = new memorystream(data); copystream(reader, context.response.outputstream);
see best way copy between 2 stream instances - c# implementation of copystream.
however in reality:
thinking second, means if zip file 500mb storing 500mb of data in memory - might fine if zip file ever in existance if there 3 or 4 of these going run out of "memory" (i.e. virtual address space).
the solution? i'm afraid easiest way save zip file (even if temporary file not directly served iis) instead of memory stream:
// save zip string filename = path.gettempfilename(); zip.save(filename); // write file context.response.transmitfile(filename);
you should delete file when done.
note need bother if determined share same zip between multiple users - if construct zip on per-user basis , write directly output stream using zip.save(outputstream)
things lot less hassle. advice first simple way , test see if performance problems can solved creating zip once.
Comments
Post a Comment