0

I have .zip file in file system. I want to download that file. So far I have done

HttpContext.Current.Response.ContentType = "application/zip";
            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename=" + FileName);
            HttpContext.Current.Response.TransmitFile(zipName);
            HttpContext.Current.Response.End();

But it directly opens up the file rather than saving it. How can download it instead if saving?

I have also seen DownloadFile(String, String) but what will be first argument in my case?

Imad
  • 7,126
  • 12
  • 55
  • 112
  • 1
    Possible duplicate of [How to force browser to download, not view, PDF documents in ASP.NET Webforms](http://stackoverflow.com/questions/18339023/how-to-force-browser-to-download-not-view-pdf-documents-in-asp-net-webforms) – fubo Oct 12 '15 at 12:08
  • You can get **[`some help here`](http://www.c-sharpcorner.com/Blogs/9383/file-download-in-Asp-Net-with-C-Sharp.aspx)** – Guruprasad J Rao Oct 12 '15 at 12:08
  • @fubo No, its not duplicate. Because my browser download excel files but not zip. So I guess browser settings is fine – Imad Oct 12 '15 at 12:09
  • try adding `` to web config > syste.webserver > staticcontent – Shaminder Singh Oct 12 '15 at 12:20

2 Answers2

1

You have to zip and than get the bytes from that zip and pass them

context.Response.AppendHeader("Content-Disposition", string.Format("attachment; filename={0}.{1}", fileName, fileExtension));
context.Response.ContentType = "application/octet-stream";
context.Response.OutputStream.Write(zipBytesArray, 0, zipBytesArray.Length);
context.Response.End();
Botea Bogdan
  • 192
  • 3
  • 16
0

In case you want to download it from the remote server then you can simply use the WebClient class

WebClient webClient = new WebClient();
webClient.DownloadFile(remoteFilePath, FileName);

or

    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
    HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
    int bufferSize = 1;

    Response.Clear();
    Response.AppendHeader("Content-Disposition:", "attachment; filename=" +filename);
    Response.AppendHeader("Content-Length", resp.ContentLength.ToString());
    Response.ContentType = "application/download";

    byte[] ByteBuffer = new byte[bufferSize + 1];
    MemoryStream ms = new MemoryStream(ByteBuffer, true);
    Stream rs = req.GetResponse().GetResponseStream();
    byte[] bytes = new byte[bufferSize + 1];
    while (rs.Read(ByteBuffer, 0, ByteBuffer.Length) > 0)
    {
        Response.BinaryWrite(ms.ToArray());
        Response.Flush();
    }
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331