Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

jquery - jqGrid Export to CSV - Post Rather than Get

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.

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.

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

It is not a problem to get the CSV, XLS or XLSX file contain per HTTP POST. The problem is to show the server response in Excel if you will use HTTP POST.

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;

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.

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.

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...