美文网首页
ajax调用php批量下载文件

ajax调用php批量下载文件

作者: geeooooz | 来源:发表于2022-03-15 13:56 被阅读0次
public function bulkDownload(){
        $ids=I("ids");
        $images = M('contract_mouldcrm')->where(['id'=>array('in',$ids)])->getField('url',true);
        $zip = new \ZipArchive;
        //压缩文件名
        $filename = 'download.zip';
        //新建zip压缩包
        $zip->open($filename,\ZipArchive::OVERWRITE);
        //把图片一张一张加进去压缩
        foreach ($images as $key => $value) {
            $zip->addFile('.'.$value);
        }
        //打包zip
        $zip->close();
        
        //可以直接重定向下载
//      header('Location:'.$filename);
        
        //或者输出下载
        header("Cache-Control: public");
        header("Content-Description: File Transfer");
        header('Content-disposition: attachment; filename='.basename($filename)); //文件名
        header("Content-Type: application/force-download");
        header("Content-Transfer-Encoding: binary");
        header('Content-Length: '. filesize($filename)); //告诉浏览器,文件大小
        readfile($filename);
        //销毁服务器zip文件
        @unlink($filename);
    }
<input id="bt1" class="btn btn-primary" value="批量下载" type="button" onClick="grant_salary()">
<script>
function grant_salary(job_id,member_id,enterprise_id){
        var id_array=new Array();
        $('input[id="ids"]:checked').each(function(){  
             id_array.push($(this).val());
        });
        alert(id_array);
        window.location.href = "http://crm.com/Crm/Contract/bulkDownload?ids="+id_array;
}
</script>

相关文章

网友评论

      本文标题:ajax调用php批量下载文件

      本文链接:https://www.haomeiwen.com/subject/gghydrtx.html