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

查看:71
本文介绍了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,在该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),这一点很重要.我个人使用打开XML生成XLSX文件的SDK 2.0 包含并将"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"设置为Content-Type.您的情况应该是"text/csv"(请参见此处).另外,可以使用Content-Disposition HTTP标头定义响应的首选文件名.例如,根据您的情况,它可以是"attachment; 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.

最重要的部分是:Web浏览器知道如何打开包含的不同URL.如果它打开新的URL(每个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天全站免登陆