所有,
我已经写了与Zend框架和MVC一个PHP5的应用程序。在我的主页,我想结合的功能,下载一个动态生成的PDF文件。在这样做的方法是:
I have a PHP5 application written with Zend Framework and MVC. On my home page, I want to incorporate the functionality to download a dynamically generated pdf file. The way this is done is:
在用户点击下载文件链接。 在点击,AJAX调用发生一个PHP控制器,它采用的形式数据,生成PDF格式,并返回一个字符串。 在我的JavaScript函数现在有PDF格式的字符串。 如何显示用户的打开/保存对话框从JavaScript?下载PDF文件我有以下的code:
<script type="text/Javascript">
$('#dlcontent').click(function(e) {
$.ajax({
type: 'POST',
url: "/downloads/dlpolicies",
data: $("#frmMyContent").serialize(),
cache: false,
dataType: "html",
success: function(html_input){
alert(html_input); // This has the pdf file in a string.
//ToDo: Open/Save pdf file dialog using this string..
}
});
});
</script>
感谢
我会尽量保持这个简单。只要透过与目标=_空白
表格,然后让PHP强制文件下载。
I would try to keep this simple. Just sumbit the form with a target="_blank"
and then have PHP force the file download.
HTML code
<script type="text/Javascript">
$('#dlcontent').click(function(e) {
e.preventDefault();
$("#frmMyContent").submit();
});
</script>
<form id="formMyContent" action="/downloads/dlpolicies" target="_blank" ... >
...
</form>
然后在你的服务器端,你需要告诉PHP来发送响应为下载。
Then on your server side, you need to tell PHP to send the response as a "download".
PHP code
$this->getResponse()
->setHeader('Content-Disposition', 'attachment; filename=result.pdf')
->setHeader('Content-type', 'application/pdf');
得到这个从这里: Zend框架如何设置页眉
Got this from here: Zend Framework how to set headers