File download script doesn't work when called from Ajax

后端 未结 5 663
青春惊慌失措
青春惊慌失措 2020-12-02 00:54

I\'m using the following script to initiate file downloads:

if (file_exists($newfilename)) {
    header(\'Content-Description: File Transfer\');
    header(\         


        
相关标签:
5条回答
  • 2020-12-02 01:18

    When I need to download a file using ajax, I have one of the following two situations:

    • The extension of the file to download is interpretable by the webserver (html, txt, pl, php), depending on how the server is configured
    • The extension of the file is another, and it exists the physical (or logical) file mapped in the URL.

    In this second case, it is enough to write a meta tag in the ajax answer, writing it into any element's innerHTML:

    <meta http-equiv = "refresh" content="0;URL='somedir/worksheet.xls'" />
    

    Since the xls extension points to an existing file it will be immediately offered to the user for download.

    In the first case, however, it is not possible to use the meta tag, since the extension is html or any other interpretable by the webserver and will be served redirecting our website.

    As it is usual practice to offer an anchor with the URL of the file to be downloaded for the case that does not start the download automatically, I apply this solution: first, I write the hiperlink

    <p> If the download does not start, 
      <a id="link_id" href='somedir/some.html'> click here</a> 
    </p>
    

    into some element of the page and then make the callback function click on the anchor element

    document.getElementById ("link_id").click();
    
    0 讨论(0)
  • 2020-12-02 01:26

    AJAX requests does not served the same that other browser HTTP request do. You need only to put a link to your script with desired parameters, using about="_blank" or something like this. Modern browsers serve that well.

    0 讨论(0)
  • 2020-12-02 01:30

    javascript cannot download files as its a security issue.

    0 讨论(0)
  • 2020-12-02 01:33

    You cannot use AJAX to download files. It doesn't make sense. You can send the AJAX request and fetch the file contents inside the success handler on the client, but for obvious security reasons you can't do much with it. You cannot save it on the client computer and there's no javascript API allowing you to prompt the user where to save it.

    So to download files, don't use AJAX. Create an anchor pointing to your server side script that serves the file to be downloaded.

    0 讨论(0)
  • 2020-12-02 01:43

    It is possible to download a file using AJAX.

    javascript:

    function exportToCsv(){
    
        var xmlhttp;
    
        if(window.XMLHttpRequest){ xmlhttp = new XMLHttpRequest; }else{ xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
        xmlhttp.onreadystatechange = function(){
            if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
                    window.location="download.php?filename=export.csv";
                }
    
            }
        }
        request = "exportToCsv.php";
        xmlhttp.open("GET", request, true);
        xmlhttp.send();
    
    }
    

    The code above, exports a mysql database of contacts to a .csv file. After that, if everything is Ok (xmlhttp.readyState == 4) automatically starts the download. window.location="download.php?filename=export.csv";

    download.php file:

    <?php
    
        $file = $_GET['filename'];
    
        header("Cache-Control: public");
        header("Content-Description: File Transfer");
        header("Content-Disposition: attachment; filename=".$file."");
        header("Content-Transfer-Encoding: binary");
        header("Content-Type: binary/octet-stream");
        readfile($file);
    
    ?>
    

    After this, the browser just presents the "Save file as" dialog, and no page refresh happens whatsoever. I have the application up and running, and never had problems. Runs on the following browsers:

    Chrome v37.0.2062.120 
    Firefox v32.0.1
    Opera v12.17
    Internet Explorer v11
    
    0 讨论(0)
提交回复
热议问题