此下载一个文件是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