webuploader上传文件,含文件大小、类型验证

var that = WebUploader.create({

        // 选完文件后,是否自动上传。
        auto: true,

        // swf文件路径
        swf: '/js/webuploader-0.1.5/Uploader.swf',

        // 文件接收服务端。
        server: 'url',

        // 选择文件的按钮。可选。
        // 内部根据当前运行是创建,可能是input元素,也可能是flash.
        pick: '#'+but,
        fileSingleSizeLimit:maxSize*1024*1024,   //设定单个文件大小

        // 只允许选择图片文件。
        accept: {
            title: 'Images',
            extensions: 'jpg,png',
            mimeTypes: 'image/*'
        }

    });
    /**
     * 验证文件格式以及文件大小
     */
    that.on("error",function (type){
        if (type=="Q_TYPE_DENIED"){
            dialogMsg("myModal","messageP","请上传JPG、PNG格式文件");
        }else if(type=="F_EXCEED_SIZE"){
            dialogMsg("myModal","messageP","文件大小不能超过8M");
        }
    });

   // $("#"+but).find(".webuploader-pick").removeClass("webuploader-pick");
   // $("#"+but).find("label").css("margin-top","0px").css("margin-bottom","0px");
   // $("#"+but).find("label").parent().css("height","30px");

    /**
    that.on( 'uploadStart', function( file ) {
        jQuery('#'+err).addClass("hide");
        if (validateFileSize(file.size,maxSize)){
        }else{
            jQuery('#'+err).html("文件大小不能超过"+maxSize+"M");
            jQuery('#'+err).removeClass("hide");
            that.stop(file);
            that.cancelFile(file.id);
        }
    });
*/
    // 当有文件添加进来的时候
    that.on( 'fileQueued', function( file ) {
       // jQuery("#"+btn).attr("disabled",true);
        jQuery('#'+err).addClass("hide");
        jQuery('#'+err).html("");
        $("#"+prevHere).html("<img>");
       // $("#"+uploadPrevpic_text).hide();
        $("#"+prevHere).show();
        $("#"+img_up).show();
        //创建缩略图
        //如果为非图片文件,可以不用调用此方法。
        var $img = $("#"+prevHere).find('img');
        that.makeThumb( file, function( error, src ) {
            if ( error ) {
                $img.replaceWith('<span>不能预览</span>');
                return;
            }
            $img.attr( 'src', src );
        }, thumbnailWidth, thumbnailHeight );

    });

    that.on('uploadBeforeSend',function (object ,data ,headers){
        headers['X-Requested-With']=  'XMLHttpRequest';
    })


    // 上传成功事件
    that.on('uploadSuccess', function (file, response) {
        // 返回的json对象
        var result = response ;
        var url = result.url ;
        var status = result.status ;

        if(status) {
            $("#"+hide_url).val(url);

        } else {
            //文件上传失败
            jQuery('#'+err).removeClass("hide");
            jQuery('#'+err).html(result.msg);
            $("#"+hide_url).val("");
            $("#"+prevHere).html("");
            $("#"+prevHere).hide();
        }

    });
    //上传失败
    that.on( 'uploadError', function( file ) {
        jQuery('#'+err).removeClass("hide");
        jQuery('#'+err).html("图片上传出错!");
        $("#"+hide_url).val("");
        $("#"+prevHere).html("");
        $("#"+prevHere).hide();
       // $("#"+uploadPrevpic_text).show();
    });
    // 完成上传完了,成功或者失败,先删除进度条。
    that.on( 'uploadComplete', function( file ) {
        jQuery("#"+but).attr("disabled",false);
        $("#"+img_up).hide();
        that.removeFile( file,true );
    });

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值