views:

60

answers:

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.

A: 

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
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
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
+1  A: 

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.

Mike Sherov
@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
@Mike you're right; I don't know what I was thinking
Pointy