Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I generate and send a .zip file to a user in C# ASP.NET?

Tags:

c#

file

asp.net

zip

I need to construct and send a zip to a user.

I've seen examples doing one or the other, but not both, and am curious if there are any 'best practices' or anything.

Sorry for the confusion. I'm going to generating the zip on the fly for the web user, and sending it to them in the HTTP response. Not in an email.

Mark

like image 798
MStodd Avatar asked May 07 '09 13:05

MStodd


1 Answers

I would second the vote for SharpZipLib to create the Zip file. Then you'll want to append a response header to the output to force the download dialog.

http://aspalliance.com/259

should give you a good starting point to achieve that. You basically need to add a response header, set the content type and write the file to the output stream:

Response.AppendHeader( "content-disposition", "attachment; filename=" + name );
Response.ContentType = "application/zip";
Response.WriteFile(pathToFile);

That last line could be changed to a Response.Write(filecontents) if you don't want to save to a temp file.

like image 97
Adam Pope Avatar answered Oct 14 '22 05:10

Adam Pope