此下載一個文件是HTML:通過點擊一個按鈕
<button name="download">Download</button>
我希望用戶下載文件時,他們單擊此按鈕。對於這一點,我用下面的Ajax請求:
$parent.find('button[name=download]').click(function() {
$.ajax({
url: "download.php",
sucess: function(data, status, jqXHR){
$console.text('Download efetuado');
},
error:function(xhr, status, error){
$console.text(xhr.responseText);
}
});
});
其中的download.php是:
if (file_exists($myFile)){
header ("Content-Type: application/json");
header ("Content-Disposition: attachment; filename=$fileName");
header("Content-Length: " . filesize("$myFile"));
$fp = fopen("$myFile", "r");
fpassthru($fp);
} else {
echo "no file exists";
};
這不是什麼outputing,沒有文件被下載。我知道PHP腳本正在工作,正在被調用。
不發送ajax請求。只需重定向到下載。或者更好的做法是,讓按鈕成爲錨標籤並刪除javascript。 –
[如何使用PHP單擊文件的名稱來下載文件?](http://stackoverflow.com/questions/4518702/how-to-download-a-file-on-clicking-the-name -of-file-using-php) – showdev