控制器中方法
/** * 下载分享码 * * @param $ids * @return void */ public function multi($ids = null) { $ids = $this->request->param('ids'); $product = new \app\admin\model\Hproblem(); $ids = explode(',', $ids); $shuju = $product->whereIn('id' , $ids)->field('attachfiles,smallimages,shipfiles')->select(); $data1 = []; foreach ($shuju as $item=> $value){ if ($value['attachfiles']==''){ continue ; } $value['attachfiles']= explode(',',$value['attachfiles']); $data1 =array_merge($data1, $value['attachfiles']); } $data2 = []; foreach ($shuju as $item2=> $value2){ if ($value2['smallimages']==''){ continue ; } $value2['smallimages']= explode(',',$value2['smallimages']); $data2 =array_merge($data2, $value2['smallimages']); } $data3 = []; foreach ($shuju as $item3=> $value3){ if ($value3['shipfiles']==''){ continue ; } $value3['shipfiles']= explode(',',$value3['shipfiles']); $data3 =array_merge($data3, $value3['shipfiles']); } $arr = array_merge($data1,$data2,$data3); foreach ($arr as $v){ // $v['shipfiles'] = substr($v['shipfiles'],31); $as = ROOT_PATH.'public/'.$v; $data4[] = $as; } $url = ROOT_PATH.'public/'; // $data = [ // ROOT_PATH.'public/uploads/QR/20230301/_18.jpg' // ]; // zip 临时文件 $tmp_dir = $url . 'temp/'; // zip名称 $zip_name = 'quality_'.time().'.zip'; // 打包 $zipName = $this->zip_files($data4, $tmp_dir.$zip_name); //下载 $down = 'http://smarthhome.shs.broing.cn//temp/'.$zip_name; downloads($down); // return json(['code'=>1]); } /** * 打包成zip * @param $pathArr * @param $zipName * @return array */ public function zip_files($pathArr,$zipName) { $zip = new \ZipArchive(); if($zip->open($zipName,\ZipArchive::CREATE|\ZipArchive::OVERWRITE)){ foreach($pathArr as $file){ if(!file_exists($file)){ continue; } $zip->addFile($file,basename($file)); //向压缩包中添加文件 } $zip->close(); return $zipName; }else{ return ''; } }
/** *下载小程序码到浏览器 * * @param string $pic_url 下载路径 */ function downloads($file) { $fileTmp = pathinfo($file); $fileExt = $fileTmp['extension']; //图片后缀名 $filename =($fileTmp['filename']); //图片名称,可以任务设置图片名称 $saveFileName = ($filename.'.'.$fileExt); $fp=fopen($file,"r"); //下载文件需要用到的头 Header("Content-type: video/quicktime"); Header("Accept-Ranges: bytes"); Header("Content-Disposition: attachment; filename=".$saveFileName); $buffer=1024; $file_count=0; //向浏览器返回数据 while(!feof($fp)){ $file_con=fread($fp,$buffer); $file_count+=$buffer; echo $file_con; } fclose($fp); }
js中
<a href="#" title="下载资源" class="btn btn-success btn-disabled download {:$auth->check('product/multi')?'':'hide'} ">下载分享码</a>
$('.download').click(function () { var ids = Table.api.selectedids($("#table")); window.location.href = '/CWLMAKSBYJ.php/hproblem/multi?ids='+ids; });