JQuery的Ajax调用对PDF文件下载 [英] JQuery Ajax call for PDF file download

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

问题描述

我的问题是类似下载并打开PDF文件>

My question is similar to Download and open pdf file using Ajax

但不完全一样,我之所以想要一个JQuery的阿贾克斯是被动态地从这将在同一页面中要获取的数据生成我的文件。

But not exactly the same , the reason I want an JQuery ajax is that my file is being generated dynamically from the data which would be fetched from the same page.

所以基本上我有一个JSON对象需要被送到这将动态生成一个文件,并将其发送回浏览器服务器。

So basically I have a Json Object which needs to be sent to the server which would dynamically generate a file and send it back to the browser.

我不希望在我的JSON对象锚s​​tringyfied使用查询字符串,
因为我认为这将是一个潜在的威胁作为查询字符串有字符限制(我说的对吗?)。

I would not like to use query strings on an anchor with my Json object stringyfied , as I think it would be a potential threat as query strings have character restrictions ( am I right here ?).

请让我知道如果我的工作流程是正确的,我也可以使用不同的流量达到相同的事情。

Please let me know If my workflow is right or I can achieve the same thing thing using a different flow.

推荐答案

您不应该使用AJAX来下载文件。这是行不通的。这是说,你只剩下2个选择:

You should not use AJAX to download files. This doesn't work. This being said you are left with 2 options:


  1. 动作链接和查询字符串参数,或者如果下载必须在你还可以设置 window.location.href 来操作一些特定的JavaScript事件被trigerred是应该生成的PDF文件,并​​通过查询字符串参数吧。

  1. action link and query string parameters or if the download must be trigerred at some specific javascript event you could also set the window.location.href to the action that is supposed to generate the PDF file and pass query string parameters to it.

如果你是怕你有大量的数据传递给控制器​​动作,将生成PDF下载您使用<形式> 方法=POST和里面你使用隐藏域存储尽可能多的参数和数据,只要你喜欢:

or if you are afraid that you have lots of data to pass to the controller action that will generate the PDF to download you use a <form> with method="POST" and inside you use hidden fields to store as much parameters and data as you like:

@using (Html.BeginForm("download", "home"))
{
    ... hidden fields containing values that need to be sent to the server
    <input type="submit" value="Download PDF" />
}


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

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