我遵循它在此處所寫的內容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