使用Jquery的字符串PDF文件下载 [英] Download pdf file from a string using Jquery

查看:133
本文介绍了使用Jquery的字符串PDF文件下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有,

我已经写了与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:

  1. 在用户点击下载文件链接。
  2. 在点击,AJAX调用发生一个PHP控制器,它采用的形式数据,生成PDF格式,并返回一个字符串。
  3. 在我的JavaScript函数现在有PDF格式的字符串。
  4. 如何显示用户的打开/保存对话框从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

这篇关于使用Jquery的字符串PDF文件下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆