1

I am able to create a zip with no problem, the only thing I cannot do, stock the zip file in a link so that when the user clicks on the link it will download the file

Response.Clear();
Response.ContentType = "application/zip";
Response.AddHeader("content-disposition", "filename=Photo.zip");

using (ZipFile zip = new ZipFile())
        {
            foreach (var pictures in pictureList)
            {
                zip.AddFile(Server.MapPath("~\\Content\\pictures\\upload\\" + pictures.name),"images");
            }
            zip.Save(Response.OutputStream);
        }
Response.End();
spnshguy
  • 21
  • 4
  • Why not? What did you try? There are many ways to do this. – Rob Dec 16 '15 at 03:47
  • well first I did Response.Clear(); Response.ContentType = "application/zip"; Response.AddHeader("content-disposition", "filename=Photo.zip"); followed by the Response.End() but the problem with that is that it does not redirect my page so I'm stuck on a page that I dont want – spnshguy Dec 16 '15 at 03:50
  • if there's a way to provide the zip file to the user and to redirect the page at the same time,it would also fix my problem atm – spnshguy Dec 16 '15 at 03:52
  • `followed by the Response.End()` - did you actually stream the file? The other parts are correct. You could also store the `zip` somewhere accessable by your application and then provide a link verbatim: `www.mywebsite.com/zipfiles/myzip.zip` and storing your zip in the folder `zipfiles` in your virtual directory – Rob Dec 16 '15 at 04:04
  • I got nothing after the response.end and could you show an example for what you're explaining to me? – spnshguy Dec 16 '15 at 04:14
  • Take a look here: http://stackoverflow.com/questions/5828315/write-pdf-stream-to-response-stream - If you're using MVC it's a lot easier than what you're doing – Rob Dec 16 '15 at 04:16

1 Answers1

0

Code below works for the file downloading.

public void DownloadFile(string fileName)
 {
    FileInfo file = new FileInfo(@"D:\DOCS\"+fileName);
    Context.Response.Clear();
    Context.Response.ClearHeaders();
    Context.Response.ClearContent();
    Context.Response.AddHeader("Content-Disposition", "attachment;  filename=" + file.Name); Context.Response.AddHeader("Content-Length", file.Length.ToString());
    Context.Response.ContentType = "application/zip"; 
    Context.Response.Flush();
    Context.Response.TransmitFile(file.FullName);
    Context.Response.End();
}

However, Calling Response.Redirect after Response.End() will not going to work. If you really want to redirect the page you might have to think of an alternative way.

chamara
  • 12,649
  • 32
  • 134
  • 210