使用ajax下载与存储名称不同名称的文件
Downloading a file with a different name to the stored name with ajax
我按照这里的内容下载一个与存储名称不同名称的文件。但我并不完全理解所有内容,因为当我尝试使用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' /></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重新测试通过成功,并在我阅读它时提醒一个不可理解的代码,这个代码肯定是pdf文件的代码。
您不能让客户端通过ajax请求下载文件。
您在这里所做的是用PHP在服务器端读取文件,并将其内容返回给调用方脚本(事实上,您可以在"data"变量中看到内容)
如果没有ajax,您可以这样调用脚本:
<a href='path/to/your/phpscript.php' target='_blank' ><img src='../tools/telecharger.gif' /></a>
它会将文件发送到浏览器
编辑:如果您将属性target="_blank"添加到锚点,它将在新选项卡中打开下载,而无需重新加载当前页面
相关文章:
- 如何在php文件中获取$.post-ajax传递的值
- 在使用Polymer'加载所有json文件后执行方法;s的核心ajax
- 通过Ajax将JavaScript函数传递给PHP文件
- Ajax文件加载和<输入>文件加载
- 正在传递JSONP标头's数据参数到另一个文件中的AJAX请求
- 通过ajax到php文件获取单选按钮值
- 在样板文件中实现Ajax
- AJAX不会在文件上传后重定向到网页-POST方法
- 如何在 AJAX JQuQudy中对文件上传进行排队
- 使用ajax下载与存储名称不同名称的文件
- Ajax调用返回当前html页面,而不是请求的文件
- 在更改名称时,使用ajax jsp在下拉菜单中填充多个文件夹的数据
- AJAX上传文件不起作用
- 使用AJAX将文件上载到服务器
- 使用ajax上传的文件返回错误,但使用c#在asp.net的服务器端正常提交的文件
- AJAX在Django的外部JS文件中发布
- YUI向json文件发出ajax请求
- WebApp 读取文本文件 Ajax、HTTPrequest 或
- Magento:无法运行数据流配置文件:Ajax 错误(未捕获的类型错误:e 不是函数)
- 如何使用链接检索整个数据文件(ajax?)