通过jQuery AJAX使用POST数据的PHP文件下载 [英] PHP File Download using POST data via jQuery AJAX

查看:117
本文介绍了通过jQuery AJAX使用POST数据的PHP文件下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我知道已经有一些<一href="http://stackoverflow.com/questions/3499597/javascript-jquery-to-download-file-via-post-with-json-data">similar职位,但我认为这是不够的变化,以保证其自身的问题:

So I know there have been a number of similar posts, but I think this is enough of a variation to warrant its own question:

我建立在PHP和jQuery的XLS出口国。我尝试后使用jQuery(我相信将是太长作为GET查询字符串)的数组,并用它来生成我的服务器,用户可以再下载的XLS文件。

I am building an XLS exporter in PHP and jQuery. I am trying to POST an array with jQuery (which I believe is going to be too long as a GET querystring), and use it to generate an XLS file on my server, which the user can then download.

我已经使用隐藏的内置页框,在过去做到这一点,但因为他们只是重定向到URL,这要求我使用GET,这让我很紧张。

I have used hidden iframes in the past to accomplish this, but since they just redirect to a url, this requires me to use GET, which makes me nervous.

我的问题是,那么:我怎么保存我的服务器,并链接到他们对这些文件,如果他们被动态生成,有可能由多个用户?请问一个隐藏的iframe链接到位于基于会话ID或类似的东西它们的文件一个单独的PHP脚本?

My question is then: how do I store those files on my server and link to them if they're being generated dynamically, potentially by multiple users? Would a hidden iframe link to a separate PHP script that locates THEIR file based on a session ID or something like that?

在此先感谢这里我敢肯定被问任何指导所有的时间:)

Thanks in advance for any guidance here on what I'm sure gets asked all the time :)

推荐答案

这是可以张贴到一个隐藏的iframe。因此,你不需要担心的查询字符串的长度;你将张贴键/值对,将生成的XLS文件,随后强制文件下载到浏览器。

It is possible to POST to a hidden iframe. Therefore, you don't need to worry about the length of the query string; you will post the key/value pairs which will generate your XLS file and subsequently force the file download to the browser.

<form method="post" action="/download/xls" target="download_xls"> 
  <fieldset>
    <label>Key 1:</label>
    <input type="text" name="key_1" />
  </fieldset>

  <fieldset>
    <label>Key 2:</label>
    <input type="text" name="key_2" />
  </fieldset>

    <fieldset>
            <input type="submit" value="Submit" /> 
    </fieldset> 
</form>

<iframe id="download_xls" name="download_xls" width="0" height="0" scrolling="no" frameborder="0"></iframe>

更新 快速谷歌搜索变成了这篇文章:<一个href="http://particletree.com/notebook/ajax-file-download-or-not/">http://particletree.com/notebook/ajax-file-download-or-not/

UPDATE A quick Google search turned up this article: http://particletree.com/notebook/ajax-file-download-or-not/

基本上,该建议是发表您的形式提供给当前页面和文件下载响应。这种替代可能是配不上你。

Basically, the suggestion is to POST your form to the current page and respond with a file download. This alternative might be good enough for you.

这篇关于通过jQuery AJAX使用POST数据的PHP文件下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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