我遵循它在此处所写的内容Downloading a file with a different name to the stored name。 但我不是太了解所有的东西,因为它不工作,当我尝试用ajax做。我表示我使用Smarty。使用ajax将名称不同的文件下载到存储的名称
这是我的html代码:
// In $attache.id their is the id of the data i want to dowload in the database
<a href='javascript:uploadAttachFile({- $attache.id -});'><img src='../tools/telecharger.gif' /></a>
这是我的jQuery代码:
function uploadAttachFile(idAttache)
{
$.ajax({
//This permit me to go to the php function
url: ajaxURL("attacheUpload"),
type: "POST",
data: {
idAttache: idAttache
},
success: function(data)
{
alert(data);
}
})
}
这是我的PHP代码:
//Permit to have the path and new title of the file
$attacheEntite = Model_AttacheDB::getNameAndPathAttachFile(request('idAttache'));
$file = 'path' . $attacheEntite['path'];
if (file_exists($file))
{
header('Content-disposition: application/force-download; filename="' .$attacheEntite['titre']. '"');
header("Content-Type: application/pdf");
header('Content-Transfer-Encoding: binary');
header("Content-Length: " . filesize($file));
header("Pragma: ");
header("Expires: 0");
// upload the file to the user and quit
ob_clean();
flush();
readfile($file);
exit;
}
我知道,它的通过我的php代码,因为ajax reuest通过成功并提醒一个不可理解的代码,当我阅读它时,它肯定是pdf文件的代码。
什么是不工作?你是否收到任何错误讯息? – Epodax
没有错误只是没有追加 – Kvasir
他们只是我的ajax请求的警报,但他们没有其他窗口问我是否要下载文件或类似的东西。 – Kvasir