I'm trying to implement an AJAX solution for exporting tabular data on a page in certain formats, and i've determined that the best way to do this (for my purposes) is to grab the innerHTML of a table, send it off to a server-side script for processing into alternate formats, and then returning the response with appropriate Content-Type and Content-Disposition headers. The issue I'm having is how exactly to get the browser to interpret said headers.
For example, I send the table's innerHTML in a POST request to a server-side script (via an XMLHTTP object):
xmlhttp.open("POST", "process.cgi", true);
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4) {
// do what we need to do
}
}
xmlhttp.send('exportdata='+escape(dataTable.innerHTML));
If someone directly requests the server-side script (ie: through a form), it sends appropriate headers so the user is prompted by the browser to download the file. For example, exporting to an Excel spreadsheet, it sends:
Content-Type: application/vnd.ms-excel
Content-Disposition: attachment; filename="1165420852.xls"
My problem is, I don't know how to get the browser to interpret those headers coming from an XMLHTTP object.