1

I'm trying to send a csv file to the client. I'm setting the right response headers as far as I know (it used to work), and the browser (Chrome, IE) is getting this:

HTTP/1.1 200 OK
Cache-Control: private, s-maxage=0
Transfer-Encoding: chunked
Content-Type: text/csv; charset=utf-8
Content-Encoding: gzip
Vary: Accept-Encoding
Server: Microsoft-IIS/8.0
X-AspNetMvc-Version: 5.0
Content-Disposition: attachment; filename="Cameras_Camcorders_Photo_Frames_Binoculars_Telescope_Surveillance_Camera_Accessories_(10-29-2013).xlsm_140217.csv"
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcbGZlcnJhb1xEb2N1bWVudHNcVmlzdWFsIFN0dWRpbyAyMDEzXFByb2plY3RzXFRlbXBsZXhcdHJ1bmtcQ29uU29sLlRlbXBsZXguV2ViQWxwaGEzXEpvYlxSdW5cMTc1YWRlY2UtYzM2MC00ZjMxLWJkM2YtMjk3ZjNkYWMwNTQ2?=
X-Powered-By: ASP.NET
Date: Mon, 17 Feb 2014 13:02:05 GMT

The form that's triggering the file download is a jQuery form. If I don't use jQuery form, the download occurs normally, if I use jQuery form, nothing happens.

Luis Ferrao
  • 1,463
  • 2
  • 15
  • 30
  • 1
    What are you doing to trigger the HTTP request? Clicking a link? Submitting a form? Using XMLHttpRequest? Something else? – Quentin Feb 17 '14 at 13:17
  • I'm using jQuery Form, I edited my question to narrow down its scope as I just noticed that without jQuery Form the download does occur – Luis Ferrao Feb 17 '14 at 13:24

1 Answers1

1

From your comments:

I'm using jQuery Form

From the jQuery Form homepage:

The jQuery Form Plugin allows you to easily and unobtrusively upgrade HTML forms to use AJAX

If you are handling the response with Ajax, then you completely bypass the normal browser mechanism for handling the HTTP response (which decides if it should render or save). It goes to JavaScript instead.

You need to use a regular form submission and not Ajax if you want Content-Disposition to trigger a save action on the response.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • But I'm intercepting the response on the success callback, can't I do something with it to trigger the file download? Or simply set the right jQuery Form target option? – Luis Ferrao Feb 17 '14 at 13:32
  • You could use JavaScript to express the response body in the form of a `data:` URI and then assign it to `location`. – Quentin Feb 17 '14 at 13:39
  • @LuisFerrao The whole idea of AJAX is being able to modify current page's HTML. You don't want that so you don't need AJAX at all. – Álvaro González Feb 17 '14 at 15:04
  • I need it because contents of my form are modified in real time and i want to keep the page as is (no postbacks) – Luis Ferrao Feb 17 '14 at 15:06