Found this topic, struggling with a similar problem. In this workaround, I ended up using:
$.post('genFile.php', {data : data}, function(url) { $("body").append("<iframe src='download.php?url="+url+"' style='display: none;'></iframe>"); });
genFile.php creates the file at an intermediate location using a randomly generated string for the file name. download.php reads the generated file, sets the MIME type and location (allows you to request the use of a predefined name instead of a random string in the actual file name), returns the contents of the file and clears it, deleting the original file.
[edit] can also share PHP code ...
download.php:
<?php $fname = "/tmp/".$_GET['url']; header('Content-Type: text/xml'); header('Content-Disposition: attachment; filename="plan.xml"'); echo file_get_contents($fname); unlink ($fname); ?>
genFile.php:
<?php $length = 12; $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; $str = substr( str_shuffle( $chars ), 0, $length ).'.xml'; $fh = fopen(('tmp/'.$str), 'w') or die("can't open file"); fwrite($fh,$_POST["data"]); fclose($fh); echo $str; ?>
source share