记录:tp5 fastadmin对列表页选中数据的多张图片进行压缩并下载。
html代码
<a href="javascript:;" class="btn btn-info btn-apple btn-disabled disabled {:$auth->check('zhuanli/zhuanli/xiazai')?'':'hide'}" title="批量下载专利证书"><i class="fa fa-check"></i> 批量下载专利证书</a>
js代码
define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
var Controller = {
index: function () {
// 初始化表格参数配置
Table.api.init({
extend: {
index_url: 'zhuanli/zhuanli/index' + location.search,
add_url: 'zhuanli/zhuanli/add',
edit_url: 'zhuanli/zhuanli/edit',
del_url: 'zhuanli/zhuanli/del',
multi_url: 'zhuanli/zhuanli/multi',
import_url: 'zhuanli/zhuanli/import',
table: 'zhuanli',
}
});
var table = $("#table");
// 初始化表格
table.bootstrapTable({
url: $.fn.bootstrapTable.defaults.extend.index_url,
pk: 'id',
sortName: 'id',
fixedColumns: true,
fixedRightNumber: 1,
search:false,
columns: [
[
{checkbox: true, rowspan: 2},
// {field: 'id', title: __('Id'), rowspan: 2},
{field: 'xuhao', title: __('Xuhao'), rowspan: 2},
{field: 'zhuanlicategory.name', title: __('Zhuanlicategory.name'), operate: 'LIKE', rowspan: 2},
{field: 'title', title: __('Title'), operate: 'LIKE', rowspan: 2},
{field: 'shenqinghao', title: __('Shenqinghao'), operate: 'LIKE', rowspan: 2},
{field: 'shenqingdate', title: __('Shenqingdate'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, rowspan: 2},
{field: 'gongkaidate', title: __('Gongkaidate'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, rowspan: 2},
{field: 'shouquandate', title: __('Shouquandate'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, rowspan: 2},
{field: 'zhuanliquanren', title: __('Zhuanliquanren'), operate: 'LIKE', rowspan: 2},
{field: 'famingren', title: __('Famingren'), operate: 'LIKE', rowspan: 2},
{field: 'zhuanlizhengshu', title: __('Zhuanlizhengshu'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images, rowspan: 2},
{field: 'shoulitongzhishu', title: __('Shoulitongzhishu'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images, rowspan: 2},
{field: 'bandengtongzhishu', title: __('Bandengtongzhishu'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images, rowspan: 2},
{field: 'images', title: __('Images'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images, rowspan: 2},
{field: 'qita', title: __('Qita'), operate: 'LIKE', rowspan: 2},
{field: 'status', title: __('Status'), searchList: {"normal":__('Normal'),"hidden":__('Hidden')}, formatter: Table.api.formatter.status, rowspan: 2},
{field: 'nianfei', title: __('年费收据'), operate: false, colspan:20},
// {field: 'createtime', title: __('Createtime')},
// {field: 'updatetime', title: __('Updatetime')},
{field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate, rowspan: 2}
],[
{field: 'images1', title: __('Images1'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
{field: 'images2', title: __('Images2'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
{field: 'images3', title: __('Images3'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
{field: 'images4', title: __('Images4'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
{field: 'images5', title: __('Images5'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
{field: 'images6', title: __('Images6'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
{field: 'images7', title: __('Images7'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
{field: 'images8', title: __('Images8'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
{field: 'images9', title: __('Images9'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
{field: 'images10', title: __('Images10'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
{field: 'images11', title: __('Images11'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
{field: 'images12', title: __('Images12'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
{field: 'images13', title: __('Images13'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
{field: 'images14', title: __('Images14'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
{field: 'images15', title: __('Images15'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
{field: 'images16', title: __('Images16'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
{field: 'images17', title: __('Images17'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
{field: 'images18', title: __('Images18'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
{field: 'images19', title: __('Images19'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
{field: 'images20', title: __('Images20'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
]
]
});
// 为表格绑定事件
Table.api.bindevent(table);
table.on('load-success.bs.table', function (data) {
$(".btn-editone").data("area", ["100%", "100%"]);
$(".btn-add").data("area", ["100%", "100%"]);
});
// 批量下载专利证书
$(document).on("click", ".btn-apple", function () {
var ids = Table.api.selectedids(table);//获取选中列的id
if(ids.length==0){
layer.alert("请选择要操作的数据");
return false;
}
window.open("zhuanli/xiazai?ids="+ids.join(','));
})
},
add: function () {
Controller.api.bindevent();
},
edit: function () {
Controller.api.bindevent();
},
api: {
bindevent: function () {
Form.api.bindevent($("form[role=form]"));
}
}
};
return Controller;
});
后端代码
/**
* 批量下载专利证书
*/
public function xiazai()
{
$ids = $this->request->get("ids");
if (!$ids) {
$this->error('请选择需要下载专利证书的数据');
}
//要打包的文件
$files = [];
foreach (explode(',', $ids) as $k => $v){
$zhuanlizhengshu = explode(',', $this->model->where('id',$v)->value('zhuanlizhengshu'));
$files = array_merge($files, $zhuanlizhengshu);
}
$zip = new \ZipArchive;
//压缩文件名
$filename = 'uploads/zhuanlizhengshu/'.date('YmdHis').'.zip';
// 使用本类,linux需开启zlib,windows需取消php_zip.dll前的注释
if ($zip->open ($filename ,\ZipArchive::OVERWRITE) !== true) {
//OVERWRITE 参数会覆写压缩包的文件 文件必须已经存在
if($zip->open ($filename ,\ZipArchive::CREATE) !== true){
// 文件不存在则生成一个新的文件 用CREATE打开文件会追加内容至zip
echo '无法打开文件,或者文件创建失败';
exit;
}
}
//循环压缩文件
foreach($files as $key => $value){
// $zip->addEmptyDir("attach");
$file = ROOT_PATH.'public'.$value;
if(file_exists($file)){
$zip->addFile($file, basename($file));
} else {
echo '检测到有文件不存在';
exit;
}
}
// 关闭
$zip->close();
//打包下载zip
header("Cache-Control:max-age=0");
header("Content-Description: File Transfer");
header("Content-disposition: attachment; filename=".basename($filename));//文件名
header("Content-Type:application/zip"); //格式为zip
header("Content-Transfer-Encoding:binary"); //这是二进制文件
header("Content-Length:".filesize($filename)); //文件大小
@readfile($filename);
exit;
}