2015-04-07 56 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

相关问题