沃梦达 / 编程问答 / php问题 / 正文

使用 POST 数据通过 jQuery AJAX 下载 PHP 文件

PHP File Download using POST data via jQuery AJAX(使用 POST 数据通过 jQuery AJAX 下载 PHP 文件)

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

问题描述

所以我知道有很多类似的帖子,但我认为这足以证明自己的问题:

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 POST 一个数组(我认为它作为 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>

更新快速谷歌搜索出现了这篇文章: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.

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

本文标题为:使用 POST 数据通过 jQuery AJAX 下载 PHP 文件

基础教程推荐