Javascript 使用ajax下载与存储名称不同的文件
我遵循这里写的东西。 但我不太理解一切,因为当我尝试使用ajax时,它不起作用。我示意我使用Smarty 这是我的html代码:Javascript 使用ajax下载与存储名称不同的文件,javascript,php,jquery,ajax,smarty,Javascript,Php,Jquery,Ajax,Smarty,我遵循这里写的东西。 但我不太理解一切,因为当我尝试使用ajax时,它不起作用。我示意我使用Smarty 这是我的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'
// 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>
这是我的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文件时提醒了一个不可理解的代码,他肯定就是pdf文件的代码。你不能让客户端通过ajax请求下载该文件 您在这里所做的是使用PHP读取文件服务器端,并将其内容返回给调用方脚本(事实上,您可以在“data”变量中看到内容) 如果没有ajax,您可以这样调用脚本:
<a href='path/to/your/phpscript.php' target='_blank' ><img src='../tools/telecharger.gif' /></a>
它会将文件发送到浏览器
编辑:如果将属性target=“\u blank”添加到锚点,它将在新选项卡中打开下载,而不重新加载当前页面什么不起作用?您是否收到任何错误消息?没有错误,没有任何附加信息。它们只是我的ajax请求的警报,但它们不是其他窗口,用于询问我是否要下载文件或类似的内容。这不是我真正想要的,但有时我不得不做一些让步。非常感谢。
<a href='path/to/your/phpscript.php' target='_blank' ><img src='../tools/telecharger.gif' /></a>