問題描述
所以我知道有很多類似的帖子,但我認為這足以證明自己的問題:
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 文件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!