POST到服务器,接收PDF,提供给用户瓦特/ jQuery的 [英] POST to server, receive PDF, deliver to user w/ jQuery

查看:195
本文介绍了POST到服务器,接收PDF,提供给用户瓦特/ jQuery的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有用户点击获取PDF格式的链接。在jQuery中,我创建了一个POST Ajax调用服务器来获取PDF。该PDF来我与正确的内容标题等,通常会导致浏览器打开Reader插件,或者允许用户保存PDF。

I have a link that the user clicks to get a PDF. In jQuery, I create a POST ajax call to the server to get the PDF. The PDF comes to me with the correct content headers etc that would normally cause the browser to open the Reader plugin, or allow the user to save the PDF.

由于我收到的PDF瓦特/ Ajax调用,我不知道该怎么做,我得到了的onSuccess回调的数据。我怎么可以给我收到到浏览器中的数据,并允许它做它的默认的事情,与PDF的反应?

Since I am getting the PDF w/ an ajax call, I'm not sure what to do with the data that I get in the OnSuccess callback. How can I give the data I receive to the browser and allow it to do its default thing with the PDF response?

推荐答案

您不需要的jQuery的。只需通过表单提交您的文章,通常,和在服务器端,添加HTTP标头

You don't need jQuery at all. Just submit your POST via a form normally, and on the server side, add the HTTP header

Content-Disposition: attachment; filename="whatever.pdf"

浏览器将尽其默认的事情。

The browser will do its default thing.

另外,如果你想更注意报告的PDF生成过程中可能出现的任何错误,你可以做到这一点。发表您的参数到你的服务器使用jQuery。在服务器上,生成的二进制内容和它缓存起来了几分钟,通过一键访问您将在用户的会话,并返回一个成功的Ajax响应页面(或者,如果有错误,返回错误响应)。如果页面回来的成功响应,它可以立即做这样的事情:

Alternately, if you want to be more careful about reporting any errors that might occur during the PDF generation, you can do this. POST your parameters to your server with jQuery. On the server, generate the binary content and cache it somewhere for a few minutes, accessible via a key that you put in the user's session, and return a "success" Ajax response to your page (or if there was an error, return an "error" response). If the page gets back a success response, it can immediately do something like:

window.location = "/get/my/pdf";

然后服务器会返回缓存的PDF内容。一定要包括内容处置头,如上面。

The server then returns the cached PDF content. Be sure to include the Content-Disposition header, as above.

这篇关于POST到服务器,接收PDF,提供给用户瓦特/ jQuery的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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