AJAX / PHP强制文件下载在浏览器

时间:2015-06-09 02:50:32

标签: php ajax

您好我试图强制下载从ZipArchive()创建的zip文件,但该文件未被下载而是在浏览器中显示/读取。请检查下面的代码。

function search(item){
    var search = new XMLHttpRequest();
    search.open("POST","download.php");
    search.onreadystatechange = function(){
        if(search.readyState === 4 && search.status === 200){
            // document.getElementById('tes').innerHTML = search.responseText;
        }
    }
    search.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    search.send("searchItem="+item);
}

下载按钮,调用搜索功能。

<button value="<?php echo "$name"; ?>" onclick="search(this.value)">Download File</button>

download.php文件中的PHP代码

<?php 
 $name = trim($_POST['searchItem']);
 if(!file_exists("images/$name/$name.zip")){
    //creating a zip file
$zip = new ZipArchive();
$zip_file = "$name".'.zip';
$zip->open("images/$name/".$zip_file, ZipArchive::CREATE);
$files = scandir("images/$name/");
for ($i=2; $i < count($files) ; $i++) { 
$fi = $files[$i];
if(file_exists("images/$name/$fi")){
    $zip->addFile("images/$name/$fi", $fi);
    }
}
$zip->close();

 //force to download the zip
    $file = "images/$name/$zip_file";
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: private",false);
    header('Content-type: application/zip');
    header('Content-Disposition: attachment; filename="'.basename($file).'"');
    readfile($file);
    // remove zip file from temp path
    unlink($file);
}
?>

3 个答案:

答案 0 :(得分:1)

以下是我用来获取zip文件的内容

 header('Content-Type: application/zip'); // ZIP file
 header('Content-Disposition: attachment; filename="downloadpackage.zip"');
 header('Content-Transfer-Encoding: binary');
 header('Pragma: public');
 header('Content-Description: File Transfer');
 echo($file);

希望有所帮助

答案 1 :(得分:0)

您无法使用javascript / ajax下载文件。你使这种方式变得比它需要的更困难。忘记AJAX,只需创建一个表单或锚标记,链接到生成ZIP文件的脚本。您的页面不会被重定向,因此没有理由进行XHR。

出于安全原因,Javascript无法访问您的文件系统,因此您无法使用它来下载文件。如果您访问的每个网站都能够将文件添加到您的计算机,该怎么办?那太糟糕了。这就是为什么你不能。

答案 2 :(得分:0)

由于您无法使用AJAX下载文件,因此最简单的解决方案是将文件名返回给Javascript并使用location.href并强制下载文件。

<?php
$name = trim($_POST['searchItem']);
if (!file_exists("images/$name/$name.zip")) {
    //creating a zip file
    $zip = new ZipArchive();
    $zip_file = "$name" . '.zip';
    $zip->open("images/$name/" . $zip_file, ZipArchive::CREATE);
    $files = scandir("images/$name/");
    for ($i = 2; $i < count($files); $i++) {
        $fi = $files[$i];
        if (file_exists("images/$name/$fi")) {
            $zip->addFile("images/$name/$fi", $fi);
        }
    }
    $zip->close();

    //force to download the zip
    $file = "images/$name/$zip_file";
    echo json_encode(['filename' => $file]); // return file name to Javascript
}
?>

然后在Javascript中我使用location.href来强制下载文件

function search(item){
    var search = new XMLHttpRequest();
    search.open("POST","check.php");
    search.onreadystatechange = function(){
        if(search.readyState === 4 && search.status === 200){
            var data = JSON.parse(search.responseText);
            window.location.href = data.filename; // force file download
        }
    };
    search.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    search.send("searchItem="+item);
}
相关问题