上传文件插件(plupload)

时间:2022-08-23 15:00:43

引入插件:

<script src="../../static/js/plupload.full.min.js"></script>

 

/*
     * 上传文件配置
     * */
    //配置uploader
    var uploader = new plupload.Uploader({
        runtimes: 'html5,flash,silverlight,html4',
        browse_button : 'uploader',
        url:'/delivery/batchSendByFile',
        flash_swf_url : '../Moxie.swf',
        silverlight_xap_url : '../Moxie.xap',
        multi_selection:false,
        filters: {
            mime_types : [ //只允许上传xls和xlsx文件
                { title : "xls files", extensions : "xls" },
                { title : "xlsx files", extensions : "xlsx" }
            ]
        },
        preinit: {
            UploadFile: function (up) {
                up.setOption('url', '/delivery/batchSendByFile');
            }
        }
    });
    uploader.bind('Error', function (uploader, errObject) {
        var message = '';
        //解除锁定
        layer.closeAll('loading');
        if (errObject.code === -600) {
            message = '上传的文件太大';
        } else if (errObject.code === -200) {
            message = '由于网络原因,文件没有上传成功';
        } else if (errObject.code === -601) {
            message = '选择的文件类型不是xls或xlsx类型';
        }
        layer.msg('上传失败,错误原因:' + message,{end:function(){
            layer.closeAll();
        }});
    });
    //选中文件触发
    uploader.bind('FilesAdded', function(uploader,files) {
        var tipElement = $('#uploadBox').find('.J_side');
        controller.uploadFlag = true;
        for(var i = 0, len = files.length; i<len; i++){
            tipElement.text(files[i].name).css('color','#e62e3a');
        }
    });
    //文件上传后触发
    uploader.bind('FileUploaded',function(uploader,file,responseObject){
        var msg = JSON.parse(responseObject.response),num;
        //解除锁定
        layer.closeAll();
        /** @namespace msg.failureNum */
        /** @namespace msg.successNum */
        num = msg.successNum + msg.failureNum;
        /** @namespace data.activityerror*/
        if(msg.activityerror){
            layer.msg(msg.activityerror);
        }else if(msg.issuccess*1 === 3){
            layer.msg('全部上传失败!');
        }else /** @namespace msg.issuccess */
        if(msg.issuccess*1 === 2){
            layer.msg('上传' + num + '条纪录,成功' + msg.successNum + '条,失败' + msg.failureNum + '条',{end:function(){
                window.location.reload();
            }});
        }else if (msg.issuccess*1 === 1){
            layer.msg('全部上传成功',{end:function(){
                window.location.reload();
            }});
        }
    });