Helpful Information
 
 
Category: Ajax and Design
AJAX File Download

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

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

Main Page

<tr>
<td>&nbsp;</td>
<td style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:11px;"><input type="hidden" name="txtquery" id="txtquery" value="" /></td>
</tr>
<tr valign="top" height="30px" align="center">
<td colspan="2" style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; font-weight:bold;">EXPORT COMPLETE: <span id="row_no"></span> Respondents match export criteria.</td>
</tr>
<tr align="center">
<td colspan="2"><input type="button" value="Download CSV file" style="background:url(images/botton_bg.jpg) repeat-x; border:solid 1px #cab063; height:22px;" onClick="download_csv();" /></td>
</tr>




Ajax Coding



<script>
function download_csv()
{
//alert(listid);
var str = document.getElementById('txtquery').value;
//alert(str);
var string = 'query='+ str;
$.ajax({
type: "POST",
url: "download_csv.php",
data: string,
cache: false,
success: function(data) {

}
});
}


</script>


code for download_csv.php

<?php
include("includes/include.php");
include("includes/function.php");

$data = '';
$query = $_POST['query'];
$dataexc = mysql_query($query);
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"Addressbook.csv\"");
while ($fetch = mysql_fetch_array($dataexc))
{
$data .= $fetch['email'].", \n";
}
echo $data;
?>

Oh Sorry.

your concept for using hidden i frame is working .
thanks










privacy (GDPR)