2

I would like to make an async GET request that returns back a document with MIME content type and cause it to bring the browser's 'Save' dialog.

Previously, I used to make a regular HTTP (non-async) call through a link and the returned response had a 'Content-Type' and 'Content-Disposition' like so:

Content-Type: text/plain
Content-Disposition: attachment; 
    filename=genome.jpeg; 
    modification-date="Wed, 12 Feb 1997 16:29:51 -0500";

Is there a way to convert this to jQuery's $.ajax() GET request?

The $.ajax method only supports the dataTypes, "xml", "html", "script", "json", "jsonp", and "text". Would my response data-type fall into one of these categories?

My request looks like this:

$.ajax({url: myUrl,
    data: params,
    type: "GET",
    success: function(data)
    {
        console.log("try to save this file!");
    },
    error: function(req, status, errThrown){
        alert("ERROR: Something happened");
    }

In the 'success' callback, I see the file contents passed back in the 'data' variable as a plain text but need the 'save' dialog to get launched on the browser.

The server IS sending back the response with the correct headers set.

Pavlo
  • 43,301
  • 14
  • 77
  • 113
Abel Vita
  • 193
  • 1
  • 3
  • 7

2 Answers2

3

There is no way I know of to get a true Ajax request to pop a save dialog up. This has nothing to do with the headers sent by the server.

If you'd like to programatically pop a save dialog box, you can use jQuery to append a hidden iframe to the page with the URL as it's src. This should pop the dialog box as necessary.

Pointy
  • 405,095
  • 59
  • 585
  • 614
Mike Sherov
  • 13,277
  • 8
  • 41
  • 62
  • @Pointy, can you please point me to an example then? As far as I'm aware, the XMLHttpRequest Object that ajax works off of won't initiate a save as dialog even if the headers direct it to do so. – Mike Sherov Jun 24 '10 at 03:07
-1

That's something the server has to do. There's nothing you can do from the client side that will force an unwilling server to set response headers.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • The server is setting the response headers but the data is returned as a parameter of the 'success' callback. I want to know how to make the 'save' dialog pop-up in the browser at this point. – Abel Vita Jun 23 '10 at 23:28
  • If that's happening, then I don't think the server is actually setting the headers properly. Use something like Firebug or TamperData to check what's actually in the HTTP request and response. – Pointy Jun 23 '10 at 23:30