I have a servlet handles downloading a CSV file for me. I set the
contentType as CSV so that when send the response back to me it will
prompt me to download the CSV file. This works fine when I submit the
page normally, however, when I send my request to the server using the
XMLHTTPRequest object it does not prompt me to download the CSV file.
I can see the contents of the CSV file when I evaluate
request.responseText but I am not sure how I can amke the browser promt
the download.
Does anybody know how I can do this?
Below is a samlple of my servlet code and my AJX code
Thanks
Chris
MyServlet
PrintWriter out = response.getWriter();
response.setContentType("text/csv");
response.addHeader("Content-Disposition", "attachment;
filename=test.csv");
out.print("test,Brian,Yellow");
My Javascript
function createCSV(url) {
//alert('hello');
if (window.XMLHttpRequest)
{ // Non-IE browsers
req = new XMLHttpRequest();
req.onreadystatechange = processStateChange;
try
{
req.open("GET", url, true);
} catch (e) {
alert(e);
}
req.send(null);
} else if (window.ActiveXObject) { // IE
req = new ActiveXObject("Microsoft.XMLHTTP");
if (req) {
req.onreadystatechange = processStateChange;
req.open("GET", url, true);
req.send();
}
}
}
function processStateChange()
{
if (req.readyState == 4)
{ // Complete
if (req.status == 200)
{ // OK response
alert(req.responseText);
} else {
alert("Problem: " + req.statusText);
}
}
}
I answered the question here: http://groups.google.com/group/ajax-world/browse_thread/thread/b7abc61c1f91376a/af58d68f0b706ae9?hl=en#af58d68f0b706ae9 if anyone wants an aswer in the future.
Eric
Nice one Eric! Thanks again
Chris
Eric.. i am having the same issue.. some how your link is not working..
can you put the correct link for solution
I answered this >2 years ago, guessing I said to post the form to a hidden iframe.
Guessing someone deleted that Google Group.
Eric
Hello sir,
i have the same problem. i am using php with ajax to download csv file.
my code is here