0

I'm trying to force the browser to download a zip file. I have read lots of posts about the same problem with many accepted answers. Still I can't make it to work. Below is the code I have written.

  var Response = _context.Response;  
  string contentDisposition = string.Format("attachment; filename=data.zip");
  string contentLength;

  using (FileStream fileStream = File.OpenRead(_context.Server.MapPath(@"~/csv/data.zip")))
  {
      contentLength = fileStream.Length.ToString(System.Globalization.CultureInfo.InvariantCulture);
  }

  Response.AddHeader("Content-Type", "application/force-download;");
  Response.AddHeader("Content-Disposition", contentDisposition);
  Response.AddHeader("Content-Length", contentLength);
  Response.AddHeader("Content-Description", "File Transfer");
  Response.AddHeader("Content-Transfer-Encoding", "binary");
  //Response.TransmitFile(_context.Server.MapPath(@"~/csv/data.zip"));
  Response.WriteFile(_context.Server.MapPath(@"~/csv/data.zip"));
  Response.Flush();
  Response.End();

The zip is not downloaded. I just see the response .

enter image description here

Headers

enter image description here

dev
  • 1,377
  • 1
  • 10
  • 28
  • 1
    have you tried Response.ContentType = "application/zip"? normally i think it should work with that contenttype – user1519979 Jan 14 '15 at 09:43
  • you are trying to download zip throu postman? – aleha_84 Jan 14 '15 at 09:44
  • I don't know if postman is not supposed to work, but the same happens with my application – dev Jan 14 '15 at 09:46
  • 1
    Go for content type of `application/zip` or `application/octet-stream`. `application/force-download` probably isn't a real mime/content type. – spender Jan 14 '15 at 09:47
  • I'd try `Content-Type` as `application/octet-stream`, it has always been good for me. Is this a direct link to the file or are you using ajax or something different? – Jcl Jan 14 '15 at 09:48
  • Yes, I'm using ajax (ExtJs). Should this be a problem? – dev Jan 14 '15 at 09:50
  • 1
    Definitely... Ajax will not initiate file downloads directly. You need to use an `iframe` or something similar. See this answer: http://stackoverflow.com/questions/3599670/ajax-file-download-using-jquery-php or this one: http://stackoverflow.com/questions/20830309/download-file-using-an-ajax-request – Jcl Jan 14 '15 at 09:50
  • Yes, that's the problem! – dev Jan 14 '15 at 09:57

0 Answers0