私は1つのPHPファイルと他のJavaScriptを持っています。 JavaScriptでは、ファイルが存在する場所の場所で変数を渡しますが、PHP側ではファイルを認識せず、適切にダウンロードしません。ファイルをダウンロードするためにajaxでファイルphpを呼び出す
のJavaScriptのコード:
var nameFile = oEvent.getParameters().listItem.getTitle();
var directory = "C:/xampp/htdocsui5launchpad/WebContent/documents/";
window.location =directory;
$.ajax({
url: 'http://localhost/ui5launchpad/WebContent/php/downloadPDF.php',
type: 'POST',
datatype: "json",
data: { album: nameFile },
success: function (response, data, xhr) {
window.location = 'http://localhost/ui5launchpad/WebContent/php/downloadPDF.php';
},
error: function (response) {
console.log("Error in PHP, downloadPDF.php ");
}
});
PHPコード:
if(isset($_POST["album"])){
$name = $_POST["album"];
}
$dir = "C:/xampp/htdocs/ui5launchpad/WebContent/documents/";
$file = $dir . $name;
if(isset($file)) {
//echo $file;
if (file_exists($file)) {
//echo $file;
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
}
あなたは、私を助けてくださいことはできますか?
ありがとうございます。
、あなたはブラウザに直接そのURLを呼び出す必要があります。 – Webinion
[ファイルをjQuery.Ajaxでダウンロードする](https://stackoverflow.com/questions/4545311/download-a-file-by-jquery-ajax) –
Pandhi Bhaumik: 私はあなたが間違っていると思います。なぜなら、私はphpのURLを持っており、パラメータなしでphpだけを呼び出すと、これは完全に機能するからです。私の問題は、 –