Reputation: 1807
I'm making an AJAX request. The returned response
is a XML.
How can I let the user save the response
as a XML file locally on success
?
$.ajax({
type: "POST",
url: url,
data: JSON.stringify(myJson),
contentType: "application/json",
dataType: format,
success: function(response)
{
console.log("Exported JSON: " + JSON.stringify(myJson));
console.log(response);
jQuery.parseXML(response);
},
error: function()
{
console.log(arguments);
alert("Export process failed.");
}
});
The format
in this case is xml
.
Upvotes: 3
Views: 3719
Reputation: 41
On success, render the response XML in a textarea or just a div and have the user copy+paste the results into a text editor. They can then save the file from the editor.
or
Instead of emitting the xml directly, provide a link to it instead.
Upvotes: 1
Reputation: 21086
Using a data URI will get you part way there.
window.open("data:text/xml;base64," + window.btoa(xmlString));
Using "application/octet-stream" instead of "text/xml" will even force a download prompt in FF and Chrome.
Unfortunately data URIs have size limits and there's probably a more clever approach using content editable and the exec 'save' or 'saveas' command.
Upvotes: 2