...

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

nehakhetan
03-29-2010, 07:14 AM
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;
?>

nehakhetan
03-29-2010, 07:41 AM
Oh Sorry.

your concept for using hidden i frame is working .
thanks



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum