jqGrid Export to CSV - Post Rather than Get
Asked Answered
S

2

1

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?

Sulfuric answered 20/4, 2011 at 0:35 Comment(3)
If you plan to award the bounty you should do this explicitly (see "How do I award the bounty?" here for details)Selfexcited
Thank, Oleg. I had assumed it was awarded when I accepted the answer.Sulfuric
You are welcome! I understand you, exactly because of that I posted you the reference which describes how award the bounty really work.Selfexcited
S
7

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.

Selfexcited answered 22/4, 2011 at 20:47 Comment(0)
Z
1

Amy, I had the same problem. I am not a programer (you will realize that by the code I will paste here :) ) but the solution I've found seems to work fine.

This a solution I had for another similar problem (not related to jqGrid).

excelExport : function(o) { 
    o = $.extend({
        exptype : "remote",
        url : null,
        oper: "oper",
        tag: "excel",
        exportOptions : {}
    }, o || {});
    return this.each(function(){
        if(!this.grid) { return;}
        if(o.exptype == "remote") {
            var pdata = $.extend({},this.p.postData);

            pdata[o.oper] = o.tag;

            var form = document.createElement("form");
            form.setAttribute("method", "post");
            form.setAttribute("action", o.url);
            form.setAttribute("target", "_blank");

            $.each( pdata, function(i, l){
                if (typeof l != 'undefined') {
                    if (typeof l == 'function') {
                        post_value = l();
                    }
                    else {
                        post_value = l;
                    }
                    var hiddenField = document.createElement("input");
                    hiddenField.setAttribute("type", "hidden");
                    hiddenField.setAttribute("name", i);
                    hiddenField.setAttribute("value", post_value);
                    form.appendChild(hiddenField);
                }
             });

            document.body.appendChild(form);    // Not entirely sure if this is necessary
            form.submit();

        }
    });
}

As you can see it creates a form and posts the data to a new page. Most people here would find a better (and more elegant) way to do this but this solution, as is, works. I need to send a lot of information to the server so a GET is not enough for me, that is why I needed to POST the data.

Hope this works for you.

JMG.

Zama answered 11/4, 2012 at 19:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.