ボタンがあり、onclick
はajax関数を呼び出します。ファイルをajaxコールでダウンロードする
は、ここで私は、ユーザー入力に基づいて、CSVファイルを作成し、私のAJAX機能
function csv(){
ajaxRequest = ajax();//ajax() is function that has all the XML HTTP Requests
postdata = "data=" + document.getElementById("id").value;
ajaxRequest.onreadystatechange = function(){
var ajaxDisplay = document.getElementById('ajaxDiv');
if(ajaxRequest.readyState == 4 && ajaxRequest.status==200){
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
ajaxRequest.open("POST","csv.php",false);
ajaxRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
ajaxRequest.send(postdata);
}
です。それが作成された後、私はダウンロードを促したり、ダウンロードを強制したりしたい(できれば強制)。私はファイルをダウンロードするには、PHPファイルの最後に次のスクリプトを使用しています。このスクリプトを別のファイルで実行すると正常に動作します。
$fileName = 'file.csv';
$downloadFileName = 'newfile.csv';
if (file_exists($fileName)) {
header('Content-Description: File Transfer');
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename='.$downloadFileName);
ob_clean();
flush();
readfile($fileName);
exit;
}
echo "done";
しかし、私はcsv.phpの終わりにそれを実行した場合、それは代わりにダウンロードする(ajaxDivに)ページにfile.csvになりの内容を出力します。
強制的にcsv.phpの最後にファイルをダウンロードする方法はありますか?
です。この議論を参照してください:http://stackoverflow.com/questions/7857878/window-location-vs-document-location – yitwail