jqGrid 导出到 CSV - 发布而不是获取 [英] jqGrid Export to CSV - Post Rather than Get

查看:23
本文介绍了jqGrid 导出到 CSV - 发布而不是获取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 Post 发送请求的 jqGrid.我有一个 php 函数,当给定 jqGrid 搜索和排序设置时,它可以返回一个 CSV 文件.而且,我已经组合了一个可以调用 exportExcel 的外部按钮,以尝试调用该 php 函数并检索 CSV.

I have a jqGrid that uses Post to send the request. I have a php function that when given the jqGrid search and sort settings can return a CSV file. And, I have put together an external button that can call exportExcel in an attempt to call that php function and retrieve the CSV.

问题是,excelExport 使用 GET 来发送数据,我仍然需要它是 POST.我查看了代码并尝试了几种方法来设置 excelExport 以通过 POST 发送其请求,但没有成功.

The problem is, excelExport is using GET to send the data, and I still need it to be POST. I looked at the code and tried a few ways to set excelExport to send its request via POST with no luck.

所以,问题是:有没有办法让 excelExport 使用 POST,或者如果我将网格重新加载到我的 php 函数中,是否有一种简单的方法可以发送与网格发送的完全相同的 POST 请求可以生成CSV吗?

So, the question is: Is there a way to get excelExport to use POST, or is there an easy way for me to send the exact same POST request that the grid would send if I were reloading it to my php function that can generate the CSV?

推荐答案

在我看来,你想要的东西是不可能的.我必须更详细地解释我的意思.

It seems to me that what you want is not possible. I have to explain more detailed what I mean.

通过 HTTP POST 获取包含的 CSV、XLS 或 XLSX 文件不是问题.如果您将使用 HTTP POST,问题是在 Excel 中显示服务器响应.

excelExport方法的代码很简单,可以看这里.excelExport 所做的只是打开一个 URL,其中将添加一些额外的参数.代码的主要部分如下

The code of excelExport method is very simple you can see it here. What excelExport do is just open an URL where some additional parameters will be added. The main part of the code is following

window.location = url;

所以所有真正有趣的东西都在服务器上实现.服务器设置一些 HTTP 标头很重要,尤其是 Content-Type,它将 HTTP 响应定义为 Excel 文件(如果无法生成 XLSX 数据,则定义为 CSV).我个人使用 打开用于生成 XLSX 文件的 XML SDK 2.0 包含并将 "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" 设置为 Content-Type.在您的情况下,它应该是 "text/csv" (请参阅 here).此外,可以使用 Content-Disposition HTTP 标头来定义响应的首选文件名.例如,它可以是附件;filename=test.csv" 在您的情况下.因为你已经有了服务器代码,所以你可能已经在代码中实现了所有的东西.

So all real interesting things are implemented on the server. It is important that the server set some HTTP headers, especially Content-Type, which define the HTTP response as Excel file (or as CSV if you can't generate XLSX data). I personally use Open XML SDK 2.0 to generate XLSX file contain and set "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" as the Content-Type. In your case it should be "text/csv" (see here). Additionally one can use Content-Disposition HTTP header to define the preferred filename of the response. For example, it can be "attachment; filename=test.csv" in your case. Because you has already the server code you have probably all the things implemented in the code.

最重要的部分是:网络浏览器知道如何打开不同的URL包含.如果它打开新的 url(per HTTP GET !!!)它将使用相应的应用程序,如 Excel 来显示它.

The most important part is: web browser knows how to open different URL contains. If it open new url (per HTTP GET !!!) it will use the corresponding application like Excel to show it.

另一方面,如果您使用 $.ajax,您可以获得每个 HTTP POST 包含的 CSV,但是您想如何解决下一个问题 - 使用数据启动 Excel?如果不使用仅在 Internet Explorer 中工作的 ActiveX 控件,我不知道简单的解决方案.

On the other side if you use $.ajax you can get the CSV contain per HTTP POST, but how you want to solve the next problem - to start Excel with the data? I don't know the simple solution without the usage of ActiveX controls working in Internet Explorer only.

所以我建议您只使用 HTTP GET.如果您不想缓存数据,可以通过设置相应的 HTTP 标头来实现.在大多数情况下,Cache-Control: max-age=0 的设置就足够了.设置 Cache-Control: private 还会关闭代理上的数据缓存,并声明数据可以被缓存,但不能与其他用户共享.您可以在以下缓存教程中找到有关该主题的更多信息.

So I recommend you just use HTTP GET. If you don't want caching of data you can do this by setting the corresponding HTTP headers. Setting of Cache-Control: max-age=0 is enough in the most cases. Setting Cache-Control: private additionally switch off caching the data on the proxy and declare that the data could be cached, but not shared with another users. More information about the subject you can find in the following Caching Tutorial.

这篇关于jqGrid 导出到 CSV - 发布而不是获取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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