tp5+webupload批量上传

文章描述:

thinkphp5+webupload批量上传

视图

html

<div id="uploader" class="wu-example">
    <div class="btns">
        <div id="picker">选择文件</div>
        <div class="div-btn">
            <div id="ctlBtn" class="btn-submit">开始上传</div>
        </div>
        <div class="div-btn">
            <div id="cancle" class="btn-close" onClick="cdialog()">关闭</div>
        </div>
    </div>
    <!--用来存放文件信息-->
    <div class="clearfix" style="overflow-y:hidden;">
        <div class="table_head clearfix">
            <table class="webuploder-table">
                <col width="150px">
                <col width="60px">
                <col width="150px">
                <col width="60px">
                <thead>
                <tr>
                    <th class="text-center">文件名</th>
                    <th class="text-center">大小</th>
                    <th class="text-center">状态</th>
                    <th class="text-center">操作</th>
                </tr>
                </thead>
            </table>
        </div>
        <div class="table_body clearfix">
            <table id="file_table" class="webuploder-table">
                <col width="150px">
                <col width="60px">
                <col width="150px">
                <col width="60px">
                <tbody id="thelist">
                </tbody>
            </table>
        </div>
    </div>
</div>

script

    $(function(){
        var chunkSize = 10*1024*1024,//分片文件大小,超出此大小会自动分片
            extensions = 'zip,rar,jpg,png,pdf',//可上传文件类型
            path = 'upload/file';//文件上传服务器存储地址
        var uploadSendData = {
            // 选择文件的按钮。可选。
            // 内部根据当前运行时创建,可能是input元素,也可能是flash.
            pick: '#picker',
            accept:{
                title: 'Files',
                extensions: extensions,
            },
            // swf文件路径
            swf: '__STATIC__/webuploader/Uploader.swf',
            // 文件接收服务端。
            server: "/index.php/Index/index/upload",
            //是否允许在文件传输时提前把下一个文件准备好
            prepareNextFile: true,
            //是否要分片处理大文件
            chunked: true,
            //分片文件每片大小
            chunkSize: chunkSize,
            //如果某个分片由于网络问题出错,允许自动重传次数
            chunkRetry: 2,
            //上传并发数
            threads: 3,
            // 防止多次上传
            duplicate : false,
            //文件上传请求的参数表,每次发送都会发送此对象中的参数
            formData: {
                uid: 0,
                md5: '',
                path: path,
                format : extensions,
                maxSize: chunkSize,
            },
        }
        webUploader_file(uploadSendData);
    })

控制器

public function upload(){

       $is_success = false;
       $message = '上传失败';

       $uid = input('uid');//文件唯一性标识
       $maxSize = input('maxSize/d', 31457280);
       $path = input('path', 'upload/file');//文件最终保存路径
       $fileName = input('name');
       $fileSize = input('size');
       $format = input('format', '*');//可上传文件类型
       $chunks = input('chunks/d');//分片总数
       $chunk = input('chunk/d');//当前分片索引



       //分片上传
       //$chunks是插件自动计算的大文件分片后总个数
       if (isset($chunks) && $chunks > 1) {

           if (!empty($uid)) {
               $data = fileUploadChunks($fileName, $uid, $chunks, $chunk, $path);
               $is_success = $data['flag'];
               $message = $data['message'];
           }
       }
       else {

           $data = $this->fileUploadOne($path);
           $is_success = $data['flag'];
           $message = $data['message'];
           $path = $data['path'];
       }

       $restlt = array(
           'is_success' => $is_success,
           'message' => $message,
           'path'=>$path
       );

       return json($restlt);
   }
public function fileUploadOne($path = 'upload/file', $upload_file_name = '')
    {
        $result = $this->fileUpload($path, $upload_file_name);
        return $result;
    }
public function fileUpload(){
        $identifier = input('identifier');// 分片上传标识
        $chunkNumber = input('chunkNumber');
        $file = request()->file('file');

        $validate = [
            'ext' => 'jpg,jpeg,png,gif,doc,docx,xls,xlsx,pdf,zip,rar'
        ];

        $info = $file->move(ROOT_PATH .'uploads');

        if($info){

            $data = array(
                'path'  =>  $info->getSaveName(),
                'flag'      =>  true,
                'message'   =>  '上传成功'
            );
            return $data;
        }else{

            echo $file->getError();
        }
    }

 

发布时间:2022/10/10

发表评论