Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Jun 2006
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    AJAX File Download

    Hi,

    I have a servlet handles downloading a CSV file for me. I set the
    contentType as CSV so that when send the response back to me it will
    prompt me to download the CSV file. This works fine when I submit the
    page normally, however, when I send my request to the server using the
    XMLHTTPRequest object it does not prompt me to download the CSV file.

    I can see the contents of the CSV file when I evaluate
    request.responseText but I am not sure how I can amke the browser promt
    the download.
    Does anybody know how I can do this?

    Below is a samlple of my servlet code and my AJX code

    Thanks

    Chris
    Code:
    MyServlet
    
    PrintWriter out = response.getWriter();
    response.setContentType("text/csv");
    response.addHeader("Content-Disposition", "attachment;
    filename=test.csv");
    out.print("test,Brian,Yellow");
    
    My Javascript
    
    function createCSV(url) {
                    //alert('hello');
                    if (window.XMLHttpRequest)
                    { // Non-IE browsers
                      req = new XMLHttpRequest();
                      req.onreadystatechange = processStateChange;
                      try
                              {
                                    req.open("GET", url, true);
                             } catch (e) {
                                    alert(e);
                      }
                      req.send(null);
                    } else if (window.ActiveXObject) { // IE
                      req = new ActiveXObject("Microsoft.XMLHTTP");
                      if (req) {
                            req.onreadystatechange = processStateChange;
                            req.open("GET", url, true);
                            req.send();
                      }
                    }
            }
    
      function processStateChange()
      {
        if (req.readyState == 4)
            { // Complete
          if (req.status == 200)
              { // OK response
            alert(req.responseText);
          } else {
            alert("Problem: " + req.statusText);
          }
        }
      }

  • #2
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    I answered the question here: http://groups.google.com/group/ajax-...58d68f0b706ae9 if anyone wants an aswer in the future.

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #3
    New to the CF scene
    Join Date
    Jun 2006
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Nice one Eric! Thanks again

    Chris

  • #4
    New to the CF scene
    Join Date
    Sep 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Eric.. i am having the same issue.. some how your link is not working..
    can you put the correct link for solution

  • #5
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    I answered this >2 years ago, guessing I said to post the form to a hidden iframe.

    Guessing someone deleted that Google Group.

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #6
    New to the CF scene
    Join Date
    Oct 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Ajax file download using php

    Hello sir,
    i have the same problem. i am using php with ajax to download csv file.
    my code is here

    Main Page

    <tr>
    <td>&nbsp;</td>
    <td style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:11px;"><input type="hidden" name="txtquery" id="txtquery" value="" /></td>
    </tr>
    <tr valign="top" height="30px" align="center">
    <td colspan="2" style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; font-weight:bold;">EXPORT COMPLETE: <span id="row_no"></span> Respondents match export criteria.</td>
    </tr>
    <tr align="center">
    <td colspan="2"><input type="button" value="Download CSV file" style="background:url(images/botton_bg.jpg) repeat-x; border:solid 1px #cab063; height:22px;" onClick="download_csv();" /></td>
    </tr>




    Ajax Coding



    <script>
    function download_csv()
    {
    //alert(listid);
    var str = document.getElementById('txtquery').value;
    //alert(str);
    var string = 'query='+ str;
    $.ajax({
    type: "POST",
    url: "download_csv.php",
    data: string,
    cache: false,
    success: function(data) {

    }
    });
    }


    </script>


    code for download_csv.php

    <?php
    include("includes/include.php");
    include("includes/function.php");

    $data = '';
    $query = $_POST['query'];
    $dataexc = mysql_query($query);
    header("Content-type: application/octet-stream");
    header("Content-Disposition: attachment; filename=\"Addressbook.csv\"");
    while ($fetch = mysql_fetch_array($dataexc))
    {
    $data .= $fetch['email'].", \n";
    }
    echo $data;
    ?>

  • #7
    New to the CF scene
    Join Date
    Oct 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Ajax file download using php

    Oh Sorry.

    your concept for using hidden i frame is working .
    thanks


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •