0

I have a C# .NET application that is writing a zipped file back to the client for download. However, the browser does not receive the file or it rejects the file. The browser does not show any notifications. I have tried it both on Firefox and Chrome.

I have captured the request and response from the client and server using Fiddler:

Request:

POST http://localhost:62526/Reports/_Report_RewardLetters HTTP/1.1
Host: localhost:62526
Connection: keep-alive
Content-Length: 228
Accept: */*
Origin: http://localhost:62526
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.36
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
DNT: 1
Referer: http://localhost:62526/Reports
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8

Response:

HTTP/1.1 200 OK
Cache-Control: private
Transfer-Encoding: chunked
Content-Type: application/octet-stream
Server: Microsoft-IIS/10.0
content-dispostion: filename=Letter.zip
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcc2FpbmlfaFxTb3VyY2VcUmVwb3NcUmVzZWFyY2hPZmZpY2VEYXNoYm9hcmRcUmVzZWFyY2hPZmZpY2VEYXNoYm9hcmRcUmVwb3J0c1xfUmVwb3J0X1Jld2FyZExldHRlcnM=?=
X-Powered-By: ASP.NET
Date: Thu, 11 Feb 2016 23:13:22 GMT

....Truncated the file contents.....

My code:

Response.Clear();
Response.ClearHeaders();
Response.ClearContent();

Response.AddHeader("content-dispostion", "filename=MyFile.zip");
Response.ContentType = "application/octet-stream";

Response.Flush();

Response.WriteFile(myfile);

Response.Flush();
Response.End();

I have tried numerous combinations of Response.Flush(), Response.Clear(), HttpContext.ApplicationInstance.CompleteRequest(), Response.BinaryWrite(), Response.TransmitFile(), etc. but none seem to work. Additionally, I have in my code the necessary checks to determine the existence of the file.

From the fiddler captures, I think there is something wrong in the encoding or the server response of the file being sent to the client whereby the browser is rejecting the file without any notification.

Thanks for your help!

windrunn3r.1990
  • 414
  • 2
  • 6
  • Youv'e checked the following: http://stackoverflow.com/questions/18477398/asp-net-file-download-from-server – Orel Eraki Feb 11 '16 at 23:48
  • Yes, and I am not sure if its the same issue. There are no exceptions raised and I can capture the file that was sent using fiddler and verify that its not corrupted. – windrunn3r.1990 Feb 12 '16 at 00:26

2 Answers2

0

Just a thought: do you need the Response.Flush statements? Setting your headers, writing your file and then calling Response.End should be enough.

Also, set the ContentType to "application/zip, application/octet-stream"

dpdragnev
  • 2,011
  • 2
  • 28
  • 55
  • Tried both suggestions but it didn't work. No, I don't really need the Flush() statements, but they cropped up while I was trying to debug the issue. Removed them now, but no difference in behaviour was noticed. – windrunn3r.1990 Feb 12 '16 at 00:11
  • Just to confirm, you are using webforms, not MVC, right? Also, how are you defining "myfile"? Is it a valid path to your file? – dpdragnev Feb 12 '16 at 16:36
0

I had made a mistake that was causing all the fuss. The view was using an Ajax form to make the request instead of a normal Html form.

I fixed the problem by changing my controller and view according to the information present here: http://geekswithblogs.net/rgupta/archive/2014/06/23/downloading-file-using-ajax-and-jquery-after-submitting-form-data.aspx

Download Excel file via AJAX MVC

Community
  • 1
  • 1
windrunn3r.1990
  • 414
  • 2
  • 6