37

I have the following function that is pulling data from a database. The ajax call is working correctly. How can I send the tab delimited data in my success function to the user? Setting the contect type to "application/vnd.ms-excel" didn't work. The alert on success shows the correctly formatted data.

     function SendToExcel() {
       $.ajax({
           type: "GET",
           url: "/Search.aspx",
           contentType: "application/vnd.ms-excel",
           dataType: "text",
           data: "{id: '" + "asdf" + "'}",
           success: function(data) {
              alert(data);
           },
           error: function (jqXHR, textStatus, errorThrown) {
              alert(jqXHR.responseText);
       }});
     }

I don't want to display the data in the browser--I want to send it to Excel.

EDIT: I found a way to do what I wanted. Instead of redirecting the users to a new page that would prompt them to save/open an Excel file, I opened the page inside a hidden iframe. That way, the users click a button, and they are prompted to save/open an Excel file. No page redirection. Is it Ajax? No, but it solves the real problem I had.

Here's the function I'm calling on the button click:

     function SendToExcel() {
        var dataString = 'type=excel' +
            '&Number=' + $('#txtNumber').val() + 
            '&Reference=' + $('#txtReference').val()

        $("#sltCTPick option").each(function (i) {
             dataString = dataString + '&Columns=' + this.value;
        });

        top.iExcelHelper.location.href = "/Reports/JobSearchResults.aspx?" + dataString;;
     }
Timo Tijhof
  • 10,032
  • 6
  • 34
  • 48
Jim
  • 11,229
  • 20
  • 79
  • 114
  • 2
    Any reason why this is a bad question? Why was this down voted? – Jim Oct 13 '08 at 19:53
  • it got Down-voted because most probably everyone believe the request from AJAX is a bad approach but with a hidden iframe its possible to get the download dialog to come up. Though you get different results with each browser. I remember IE being the one with the issues. but the hidden iframe approach works - i can confirm that. – IEnumerator Jan 25 '11 at 16:28

4 Answers4

13

AJAX is... the wrong choice. Redirect the user to a server resource that will send the data down with the proper MIME type, and let the browser figure out what to do with it.

Shog9
  • 156,901
  • 35
  • 231
  • 235
6

in HTML I have a Form with serial inputs elements and a button that calls a JavaScript function onclick="exportExcel();


then in JavaScript file:
function exportExcel(){
    var inputs = $("#myForm").serialize();
    var url = '/ajaxresponse.php?select=exportExcel&'+inputs;
    location.href = url;
}

and finally a pivot file who response to something

PHP code:

case 'exportExcel':{
                     ob_end_clean();
                     header("Content-type: application/vnd.ms-excel");
                     header("Content-Disposition: attachment;
                     filename=exportFile.xls");
                     echo $html->List($bd->ResultSet($_GET));
                }

$html is an object who handle html, and $bd is an object that returns data from Database send your own html table or whatever you want.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
fernando
  • 1
  • 1
  • 1
2

Since it uses JavaScript, AJAX is bound by JavaScript's designed limitations, which includes interacting with other processes on the client's machine. In this case, it's a good thing; you wouldn't want a site to be able to automatically load an Excel document with a malicious macro in it.

If you want to display the data in the browser, you can use AJAX; otherwise, you'll want to just give a link to an Excel document and let the browser's regular download handling capabilities figure out what to do.

Randy
  • 3,972
  • 19
  • 25
0

It's possible that you don't want to do this with javascript.

What I think you want to do is create a response page with the mine type application/csv then redirect the user to that page. I would probably do a window.open() since the user doesn't lose the page they're currently on.

Sugendran
  • 2,099
  • 1
  • 14
  • 15