簡體   English   中英

從Ajax調用時,文件下載腳本不起作用

[英]File download script doesn't work when called from Ajax

我正在使用以下腳本來啟動文件下載:

if (file_exists($newfilename)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($newfilename));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($newfilename));
    ob_clean();
    flush();
    readfile($newfilename);
    exit;
}

當我直接打開頁面時,它工作正常,但問題是,我需要從另一個頁面通過Ajax調用此腳本。 當我這樣做,然后下載沒有開始。 腳本的其余部分完成了它應該做的事情。

我認為問題是無法以這種方式使用頭功能,但肯定有辦法讓這個工作嗎?

如果它有任何幫助,這是Ajax函數:

<script type="text/javascript">
    // function create GetXmlHttpObject
    function GetXmlHttpObject()
    {
    if (window.XMLHttpRequest)
    {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    return new XMLHttpRequest();
    }
    if (window.ActiveXObject)
    {
    // code for IE6, IE5
    return new ActiveXObject("Microsoft.XMLHTTP");
    }
    return null;
    }

    function submitVideoAjax(){
    var myAjaxPostrequest=new GetXmlHttpObject();

    var t2_title=document.video_form.title.value;

    var parameters="title="+t2_title;

    myAjaxPostrequest.open("POST", "newdownloadmanager.php", true);
    myAjaxPostrequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    myAjaxPostrequest.send(parameters);
    myAjaxPostrequest.onreadystatechange=function(){
    if(myAjaxPostrequest.readyState==4){
    if(myAjaxPostrequest.status==200){
    document.getElementById("result").innerHTML=myAjaxPostrequest.responseText;
    document.getElementById("video_form").style.display = "none";

    }
    else    {
    document.getElementById("video_form").innerHTML="An error has occured making the request";
    }
    }
    }
    }
    </script>

這是形式:

<form name='video_form' id='video_form' method="post">
<input type="hidden" name="title" id="title" value="Madelyn2-01.mp4"/>
<button type="button" name="submit_video" id="submit_video" onclick="submitVideoAjax();">Download</button>
</form>

您無法使用AJAX下載文件。 這沒有意義。 您可以發送AJAX請求並在客戶端上的成功處理程序中獲取文件內容,但出於明顯的安全原因,您無法對其進行太多操作。 您無法將其保存在客戶端計算機上,並且沒有javascript API允許您提示用戶將其保存在何處。

所以要下載文件,不要使用AJAX。 創建一個指向服務器端腳本的錨點,該腳本提供要下載的文件。

可以使用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();

}

上面的代碼,將聯系人的mysql數據庫導出到.csv文件。 之后,如果一切正常(xmlhttp.readyState == 4)自動開始下載。 window.location="download.php?filename=export.csv";

download.php文件

<?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);

?>

在此之后,瀏覽器只顯示“將文件另存為”對話框,並且不會發生任何頁面刷新。 我有應用程序啟動並運行,從來沒有遇到過問題。 在以下瀏覽器上運行:

Chrome v37.0.2062.120 
Firefox v32.0.1
Opera v12.17
Internet Explorer v11

當我需要使用ajax下載文件時,我有以下兩種情況之一:

  • 要下載的文件的擴展名可由Web服務器(html,txt,pl,php)解釋,具體取決於服務器的配置方式
  • 文件的擴展名是另一個,它存在於URL中映射的物理(或邏輯)文件。

在第二種情況下,在ajax答案中編寫元標記就足夠了,將其寫入任何元素的innerHTML:

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

由於xls擴展指向現有文件,因此將立即提供給用戶進行下載。

然而,在第一種情況下,不可能使用元標記,因為擴展名是html或網絡服務器可解釋的任何其他擴展,並將被重定向到我們的網站。

由於通常的做法是提供一個錨點,其中包含要自動下載的文件的URL,而不是自動開始下載,我應用此解決方案:首先,我編寫hiperlink

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

進入頁面的某個元素然后使回調函數單擊錨元素

document.getElementById ("link_id").click();

javascript無法下載文件作為安全問題。

AJAX請求與其他瀏覽器HTTP請求的作用不同。 您只需要使用about="_blank"或類似的東西為您的腳本添加所需參數的鏈接。 現代瀏覽器很好用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM