2015-04-07 69 views
-1

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

+0

什麼是不工作?你是否收到任何錯誤訊息? – Epodax

+0

沒有錯誤只是沒有追加 – Kvasir

+0

他們只是我的ajax請求的警報,但他們沒有其他窗口問我是否要下載文件或類似的東西。 – Kvasir

回答

1

您不能讓客戶端通過ajax請求下載文件。

什麼,你在這裏做的是讀取文件服務器端使用PHP和他的內容返回給調用腳本(其實你看到在「數據」變量的內容)

沒有Ajax,你可以調用劇本是這樣的:

<a href='path/to/your/phpscript.php' target='_blank' ><img src='../tools/telecharger.gif' /></a> 

它將文件發送到瀏覽器

編輯:如果您添加屬性目標=「_空白」到主播它將在新標籤中打開下載,無需重新加載當前頁面

+0

這不是我真正想要的,但有時我必須做一些讓步。謝謝。 – Kvasir

+0

http://stackoverflow.com/a/15970037/329367 – Darren

相關問題