0

I need to download a zip file using ajax and ashx. Currently I am using the code https://stackoverflow.com/a/23797348 but the downloaded zip file is invalid.

The ashx code i am using is :

MemoryStream exportPrjtResponseMemStream = SomeClass.ReturnResponse(ID);

Response.ContentType = "application/zip";

Response.AppendHeader("content-disposition", "attachment; filename=\"" + fileName + ".zip\"");

Response.CacheControl = "Private";

Response.AppendHeader("Content-Length", exportPrjtResponseMemStream.Length.ToString());                            
exportPrjtResponseMemStream.WriteTo(Response.OutputStream);

Response.Flush();

Response.End();

Please suggest.

Community
  • 1
  • 1
Praveen
  • 263
  • 2
  • 10
  • please format you code using ctrl+k. it's unreadable – kpblc Feb 05 '15 at 08:24
  • @MichalHainc there is no delta between the two files both are of same size. – Praveen Feb 05 '15 at 10:17
  • @MichalHainc: I have recieved the stream correctly in browser but somehow it gets corrupted while downloading. When I am passing the url as SRC in IFrame it is downloading correctly but the file corrupts if I am using ajax request. – Praveen Feb 05 '15 at 13:00

1 Answers1

0

My guess is the ResponseStream is not being properly built. Also try setting all the headers as per the solution in your link above.

HTH!

Community
  • 1
  • 1
amitthk
  • 1,115
  • 1
  • 11
  • 19
  • Yes i am setting the headers correctly also i am getting the memory stream in browser response but it is not downloading. – Praveen Feb 06 '15 at 05:48
  • Hi, Can you remove `Response.End()` and try? Why I asked "Response.BufferOutput=true" is because you don't need to call `Response.End()` after `Response.Flush()`. Otherwise, use `Response.Clear(); exportPrjtResponseMemStream.WriteTo(Response.OutputStream); Response.End(); ` – amitthk Feb 06 '15 at 06:40
  • Also, please be careful about the case sensitivity of the headers. I mean `Response.CacheControl="private"`. I see you mention your code is working with iframe, otherwise I would suggest manual writing of MemoryStream as in [this link](http://support.microsoft.com/kb/812406) – amitthk Feb 06 '15 at 06:56