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

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

问题描述

所以我知道有一些类似的帖子,但我认为这是足够的一个变体,以保证自己的问题:

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.

我以前使用了隐藏的iframe来完成这个,但是由于他们只是重定向到一个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链接到一个单独的PHP脚本,根据会话ID或类似的东西来定位他们的文件?

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 :)

推荐答案

可以POST到一个隐藏的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>

更新
一篇快速的Google搜索引发了这篇文章: http://particletree.com/notebook/ajax-file-download-or-不是/

基本上,建议是将您的表单POST到当前页面,并通过文件下载进行响应。这个选择可能对你来说足够好。

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.

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

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