I want to make an AJAX call to PHP function that creates a txt file and download it.
if (isset($_POST["download_file"]))
{
$filename = "sequence.txt";
$f = fopen($filename, 'w');
$content="q";
fwrite($f, $content);
fclose($f);
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Length: ". filesize("$filename").";");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Type: application/octet-stream; ");
header("Content-Transfer-Encoding: binary");
readfile($filename);
}
This is my PHP code and here is AJAX code
$.ajax(
{
url:"./apis/download_seq.php",
data:{download_file:"true",sequence:seq},
type: 'POST',
success:function(data){
}
});
And this is AJAX call. Problem is that my current code saves file on server. Like it create sequence.txt file in directory where my PHP file is located. I want to download it to client side i.e. to my browser.