0

I'm working on the client-side portion of a project and have a situation that currently recieves a block of XML data from the server, and I'm trying to take that information and prompt a user download with it. Is this possible?

Here's my code:

 var holder =
    jQuery.ajax({
                type: "POST",
                url: "xml_comes_back.json",
                data: {param : info,
                        param2 : info2
                },
                dataType: ($.browser.msie) ? "text" : "xml",
                success: function(data) { 
                    $('#selector').prev().empty().html('<a href="' + data + '" id="download">Ready for Download</a>');

                    $('#download').click(function() {
                       //Prompt download for xml stored in 'data' here
                    });


                },
                error:function (xhr, ajaxOptions, thrownError) {
                   alert('error')

                }
            });
}

I'm having major issues trying to achieve this. Is it possible to have this file download without the server create and store it by a URL? I want to just recieve the data dynamically with the proper headers and have the download link work.

Here is the response header coming back from the ajax call success:

Content-Disposition:attachment; filename=theFile.xml
Content-Type:application/force-download;charset=UTF-8
streetlight
  • 5,968
  • 13
  • 62
  • 101
  • You didn't mention what error are you getting or how is it not working. – AC1 Jan 31 '13 at 18:06
  • Sorry -- I am basically getting the correct data back but the link doesn't do anything and I have no idea how to get to the next step! – streetlight Jan 31 '13 at 18:13

2 Answers2

0

Link them to a page that generates the file, including the content-type header so it forces them to download it (or redirect them with JavaScript). I believe modern browsers will simply download it without actually opening a new window.

eds
  • 449
  • 2
  • 10
0

If the information you get from ajax is a url, than just redirect the user using "window.location = data" to a page which will have the proper content-type and content-disposition that will force the user to download. It wont open a new tab/window, I think.

  • The issue is that I'm getting back the pure XML from the ajax call, without a URL. Is there a way to send them there? – streetlight Jan 31 '13 at 20:10
  • As ajax requests a download from the server, the server will make a temporary file (with an id stored as session/database/as you want to store), and send the id to the client as xml. The client will use the id to redirect to the download file with the id it, download.file?id=1234. –  Jan 31 '13 at 21:32