简体   繁体   English

在ajax调用后强制下载

[英]Force download after ajax call

I have a php script that is waiting for ajax calls at 我有一个PHP脚本正在等待ajax调用

www.mydomain.com/file.php www.mydomain.com/file.php

The ajax call contains some variables, that the file.php will use to generate some csv results, ajax调用包含一些变量,file.php将使用这些变量来生成一些csv结果,

Would it be possible for the user to be able to download the csv content produced by file.php without having to save it somewhere on the server and stuff? 用户是否可以下载file.php生成的csv内容,而不必将其保存在服务器和其他内容上?

I don't want to redirect the user to any other page either, I want them to click a button then see the download dialog, and let them download the csv file. 我也不想将用户重定向到任何其他页面,我希望他们单击​​按钮,然后看到下载对话框,然后让他们下载csv文件。

Sure, in the PHP file before you output anything, set the content-type to something the browser will download: 当然,在输出任何内容之前,请在PHP文件中将content-type设置为浏览器将下载的内容:

header("Content-Type: application/octet-stream");

Additionally this is a good practice to include too (and lets you suggest a file name): 此外,这也是一个很好的做法,也可以包括在内(并让您建议一个文件名):

header("Content-Disposition: attachment; filename=somefile.csv;");

Depending on the browser sometimes only the later is needed, but I generally use both to make sure. 根据浏览器的不同,有时仅需要更新的浏览器,但我通常会同时使用两者来确保。

Putting elements in a form and submitting them just seems to hacky, and I don't feel comfortable using it. 将元素放在表单中并提交它们似乎很麻烦,而且我对使用它感到不舒服。

When you are using these types of things I think you are risking a bit, the next versions of browsers may simply not support them. 当您使用这些类型的东西时,我认为您会冒一点风险,下一代版本的浏览器可能根本不支持它们。

Besides I am passing complicated arrays as options in my ajax call to the server, and it's not easy to convert them all into an html form, unless I serialize the arrays in a hidden element and unserialize it on the server side, but that's all too complex. 此外,我将复杂的数组作为选项传递给服务器的ajax调用,除非将数组序列化为隐藏元素并在服务器端反序列化,否则将它们全部转换为html格式并不容易。复杂。

What I did instead was, when the ajax call is made, the server stores the output in a session, then it returns a unique key for that value, another page on the server will simply echo the output when that key is given to it as an input, 相反,我所做的是,在进行ajax调用时,服务器将输出存储在会话中,然后返回该值的唯一键,当将该键提供给它时​​,服务器上的另一个页面将简单地回显输出。输入,

So user clicks on something, then an ajax call is made, then the server stores that in a session, then user clicks on a download link and then the server removes that session. 因此,用户单击某些内容,然后进行ajax调用,然后服务器将其存储在会话中,然后用户单击下载链接,然后服务器删除该会话。

It may not be the most perfect solution specially since the user has to click twice, but it just seems more standard to me. 特别是因为用户必须单击两次,它可能不是最完美的解决方案,但是对我来说,这似乎更标准。

@user893730- Why is putting elements in a form and submitting them "hacky?" @ user893730-为什么将元素放入表单并提交为“ hacky”? Also, I don't see why serializing and deserializing array data is "complicated"--there are a billion libraries out there that support precisely that sort of thing. 而且,我不明白为什么对数组数据进行序列化和反序列化会“复杂化”-那里有十亿个库正好支持这种事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM