Ajax调用后强制下载


                    

我有一个php脚本正在等待ajax调用</ p>

www.mydomain.com/file.php </ p>

ajax调用包含一些变量,file.php将使用这些变量来生成一些csv结果,</ p>

用户是否可以下载file.php生成的csv内容,而不必将其保存在服务器和其他内容上?</ p>

我也不想将用户重定向到任何其他页面,我希望他们单击​​一个按钮,然后看到下载对话框,然后让他们下载csv文件。</ p>
     </ div>

展开原文

原文

I have a php script that is waiting for ajax calls at

www.mydomain.com/file.php

The ajax call contains some variables, that the file.php will use to generate some csv results,

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?

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.

3个回答


当然,在输出任何内容之前,在PHP文件中,将content-type设置为浏览器将下载的内容:</ p>

  header(“ Content-Type:application / octet-stream”);
</ code> </ pre>

此外,这也是一个很好的做法(包括文件名):</ p>

  header(“ Content-Disposition:附件; filename = somefile.csv;”);
</ code> </ pre>

根据浏览器的不同,有时只需要使用后者,但是我通常会同时使用两者来确定。</ p>
     </ div>

展开原文

原文

Sure, in the PHP file before you output anything, set the content-type to something the browser will download:

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.


@ user893730-为什么将元素放入表单并将其提交为“ hacky”? 另外,我不明白为什么对数组数据进行序列化和反序列化会“复杂”-那里有十亿个库正好支持这种事情。</ p>
     </ div>

展开原文

原文

@user893730- Why is putting elements in a form and submitting them "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.


将元素放入表单中并提交它们似乎很hacky,我对此感到不舒服。</ p>

当您使用这些类型的东西时,我认为您会冒一点风险,下一代版本的浏览器可能根本不支持它们。</ p>

此外,在我的ajax调用中,我将复杂的数组作为选项传递给服务器,除非将数组序列化为隐藏元素并在服务器端反序列化,否则将它们全部转换为html格式并不容易。 太复杂了。</ p>

我所做的是,当进行ajax调用时,服务器将输出存储在会话中,然后返回该值的唯一键,当给出该键时,服务器上的另一页将简单地回显输出。 作为输入,</ p>

因此,用户单击某项,然后进行ajax调用,然后服务器将其存储在会话中,然后用户单击下载链接,然后服务器删除该会话。</ p>

这可能不是最完美的解决方案,特别是因为用户必须单击两次,但这对我来说似乎更标准。</ p>
     </ div>

展开原文

原文

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.

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,

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.

It may not be the most perfect solution specially since the user has to click twice, but it just seems more standard to me.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问