私はPHPを使用してクライアントコンピューターにダウンロードを強制しようとしています(ファイルダイアログで-不吉なことは何もありません)。PHPスクリプトからの応答を制御するためにheader()関数を使用することを推奨するページをたくさん見つけましたが、これではうまくいきません。私が持っているコードは次のとおりです。
$file = $_POST['fname'];
if(!($baseDir . '\\AgcommandPortal\\agcommand\\php\\utils\\ISOxml\\' . $file)) {
die('File not found.');
} else {
header('Pragma: public');
header('Content-disposition: attachment; filename="tasks.zip"');
header('Content-type: application/force-download');
header('Content-Length: ' . filesize($file));
header('Content-Description: File Transfer');
header('Content-Transfer-Encoding: binary');
header('Connection: close');
ob_end_clean();
readfile($baseDir . '\\AgcommandPortal\\agcommand\\php\\utils\\ISOxml\\' . $file);
}
私はこのJavaScriptを使用してそれを呼んでいます:
$.ajax({
url: url,
success: function(text) {
var req = new XMLHttpRequest();
req.open("POST", 'php/utils/getXMLfile.php', true);
req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
req.send('fname=' + encodeURIComponent(text));
}
});
これにより、ファイルの内容がテキストとして返されますが、ダウンロードダイアログはトリガーされません。誰か提案はありますか?