PDA

View Full Version : AJAX File Download


bboyle18
06-14-2006, 07:05 PM
Hi,

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);
}
}
}

A1ien51
06-14-2006, 09:57 PM
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

bboyle18
06-15-2006, 12:03 AM
Nice one Eric! Thanks again

Chris

venky412
09-11-2008, 07:15 PM
Eric.. i am having the same issue.. some how your link is not working..
can you put the correct link for solution

A1ien51
10-17-2008, 05:04 PM
I answered this >2 years ago, guessing I said to post the form to a hidden iframe.

Guessing someone deleted that Google Group.

Eric