多文件下载按钮插件

项目中遇到要提供各种下载模板的问题,于是写了一个

  • App/Admin/Extensions/Tools/DownloadBtn.php
<?php

namespace App\Admin\Extensions\Tools;

use Encore\Admin\Admin;
use Encore\Admin\Grid\Tools\AbstractTool;
use Route;

class DownloadBtn extends AbstractTool
{

    /**
     * @var   [
    //下载的文件标题和下载链接
    [
     * [
        'title' => '',
        'url'   => '',
        'intro' => '',
        'rename' =>'',
       ],
     * ...
    ];
     */
    protected $downloadFiles = [];

    public function __construct($downloadFiles)
    {
        $this->downloadFiles = $downloadFiles;
    }

    protected function script()
    {
        return <<<EOT

EOT;
    }

    public function render()
    {
        Admin::script($this->script());
        $data = collect();
        $data->files = $this->downloadFiles;
        $data->title = '请点击您要下载的模板文件';
        return view('admin.tools.downloadbtn', compact('data'));
    }
}
  • 模板文件 app/resources/views/admin/tools/downloadbtn.blade.php
<div class="btn-group pull-right" style="margin-right: 10px">
    <a href="" class="btn btn-sm btn-success" data-target="#download_file_dialog"  data-toggle="modal">
        <i class="fa fa-file-excel-o"></i>  下载
    </a>
</div>
<div class="modal fade" id="download_file_dialog" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                    ×
                </button>
                <h4 class="modal-title" id="myModalLabel">
                    {!! $data->title !!}
                </h4>
            </div>
            <div class="modal-body">
                <ul>
                @foreach ($data->files as $item)
                    <li>
                        <a href="{!! $item['url'] !!}" target="_blank" download="{!! $item['rename'] !!}"><h5>{!! $item['title'] !!}</h5></a>
                        <div class="panel panel-danger">
                            <div class="panel-heading">
                                注意事项
                            </div>
                            <div class="panel-body">
                                {!! $item['intro'] !!}
                            </div>
                        </div>
                    </li>
                @endforeach
                </ul>
            </div>
            <div class="modal-footer">
                 <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
             </div> 
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>
  • 使用
use App\Admin\Extensions\Tools\DownloadBtn;

 ...

$grid->tools(function ($tools) {
    $tools->append(new DownloadBtn(
                    [
                        [
                            'title' => '客户导入模板文件(xls)',
                            'url'   => '/doc/guests_template.xls',
                            'intro' => '<ul><li>请注意,日期统一格式为: YYYY-MM-DD</li><li> 身份证号码错误的客户将会被忽略!</li></ul>',
                            'rename' => '客户导入模板.xls',
                        ],
                        [
                            ... //其它下载文件
                        ],
                    ]

                ));
});

注意: 因为pjax的问题 , 标签中的链接需加上 target=blank , 否则会直接打开文件,而不是下载