作者 开飞机的舒克

批量下载

... ... @@ -183,27 +183,6 @@ class Study extends Backend
}
}
public function down_all()
{
$data = input();
$path_arr = [];
foreach ($data['data'] as $k => $v) {
$path_arr[] = public_url() . $v['barcode'];
}
makeZip($path_arr);
$id_arr = array_column($data['data'], 'id');
$res = \app\admin\model\Study::where('id', 'in', $id_arr)->update(['down' => 1]);
if (!$res) {
$this->success('');
}
}
public function down_file()
{
downloadFile(public_url() . '/barcode.zip');
}
/**
* 添加
... ... @@ -312,7 +291,7 @@ class Study extends Backend
$ids = explode(',', $ids);
$arr = $product->whereIn('id' , $ids)->field('barcode')->select();
foreach ($arr as $v){
$v['barcode'] = substr($v['barcode'],20);
//$v['barcode'] = substr($v['barcode'],20);
$as = ROOT_PATH.'public/'.$v['barcode'];
$data[]=$as;
}
... ... @@ -323,11 +302,11 @@ class Study extends Backend
// zip 临时文件
$tmp_dir = $url . 'temp/';
// zip名称
$zip_name = 'quality_'.time().'.zip';
$zip_name = 'images'.time().'.zip';
// 打包
$zipName = $this->zip_files($data, $tmp_dir.$zip_name);
//下载
$down = 'http://campus.shs.broing.cn/temp/'.$zip_name;
$down = 'http://campus.cn/temp/'.$zip_name;
downloads($down);
}
... ...
... ... @@ -10,8 +10,7 @@
<a href="javascript:;" class="btn btn-success btn-add {:$auth->check('study/add')?'':'hide'}" title="{:__('Add')}" ><i class="fa fa-plus"></i> {:__('Add')}</a>
<a href="javascript:;" class="btn btn-success btn-import {:$auth->check('你的权限规则')?'':'hide'}" title="导入" id="btn-import-file" data-url="ajax/upload" data-mimetype="csv,xls,xlsx" data-multiple="false"><i class="fa fa-upload"></i> 导入</a>
<a href="javascript:;" class="btn btn-danger btn-export {:$auth->check('study/export')?'':'hide'}" title="{:__('Export')}" id="btn-export-file"><i class="fa fa-download"></i> {:__('Export')}</a>
<a class="btn btn-info btn-disabled disabled btn-selected" href="javascript:;"><i class="fa fa-leaf"></i> 批量下载</a>
<a href="#" title="下载分享码" class="btn btn-success btn-disabled download {:$auth->check('study/multi')?'':'hide'} ">下载分享码</a>
<a href="#" title="下载分享码" class="btn btn-success btn-disabled download {:$auth->check('study/multi')?'':'hide'} ">下载条形码</a>
<div class="dropdown btn-group {:$auth->check('study/multi')?'':'hide'}">
<ul class="dropdown-menu text-left" role="menu">
... ...
... ... @@ -512,67 +512,6 @@ EOT;
return $icon;
}
//添加方法
if (!function_exists('downloadFile')) {
function downloadFile($filePath, $readBuffer = 1024)
{
//检测下载文件是否存在 并且可读
if (!is_file($filePath) && !is_readable($filePath)) {
return false;
}
//设置头信息
//声明浏览器输出的是字节流
header('Content-Type: application/octet-stream');
//声明浏览器返回大小是按字节进行计算
header('Accept-Ranges:bytes');
//告诉浏览器文件的总大小
$fileSize = filesize($filePath);//坑 filesize 如果超过2G 低版本php会返回负数
header('Content-Length:' . $fileSize); //注意是'Content-Length:' 非Accept-Length
//声明下载文件的名称
header('Content-Disposition:attachment;filename=' . basename($filePath));//声明作为附件处理和下载后文件的名称
//获取文件内容
$handle = fopen($filePath, 'rb');//二进制文件用‘rb’模式读取
while (!feof($handle)) { //循环到文件末尾 规定每次读取(向浏览器输出为$readBuffer设置的字节数)
echo fread($handle, $readBuffer);
}
fclose($handle);//关闭文件句柄
exit;
}
}
function makeZip($datalist = [])
{
$filename = 'images.zip'; //最终生成的文件名(含路径)
if (file_exists($filename)) {
unlink($filename);
}
//重新生成文件
$zip = new ZipArchive();
if ($zip->open($filename, ZIPARCHIVE::CREATE) !== TRUE) {
exit('无法打开文件,或者文件创建失败');
}
// $datalist=array('try.php','zip_class.php');
foreach ($datalist as $val) {
if (file_exists($val)) {
$path_parts = pathinfo($val);
$zip->addFile($val, $path_parts['basename']);
}
}
$zip->close();//关闭
if (!file_exists($filename)) {
exit('无法找到文件'); //即使创建,仍有可能失败
}
return $filename;
}
function public_url($path = '')
{
$upload_path = str_replace('\\', '/', ROOT_PATH);
return $upload_path . 'public' . $path;
}
/**
*下载小程序码到浏览器
*
... ...
... ... @@ -21,23 +21,6 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
});
var table = $("#table");
//批量下载
$(document).on("click", ".btn-selected", function () {
$.ajax({
type: 'post',
url: 'study/down_all',
data: {data: table.bootstrapTable('getSelections')},
success: function (res) {
if (res.code === 1) {
var domain = window.location.href.split(':')[0] + '://' + document.domain;
var url = domain + '/back.php/study/down_file';
window.location.href = url;
location.reload();
$(".btn-refresh").trigger("click");
}
}
});
});
//导出功能
$(document).on("click", ".btn-export", function () {
... ...
不能预览此文件类型
不能预览此文件类型