关于plupload的使用(代码经过测试可以使用)

首先如何导入plupload

其中url参数:upload.php如下所示:

 "您还未选择图片"));
        exit;
    }
    $type = strtolower(substr(strrchr($name, '.'), 1)); //获取文件类型

    if (!in_array($type, $typeArr)) {
        echo json_encode(array("error" => "请上传jpg,png或gif类型的图片!"));
        exit;
    }
    if ($size > (50000 * 1024)) { //上传大小
        echo json_encode(array("error" => "图片大小已超过50000KB!"));
        exit;
    }

    $pic_name = time() . rand(10000, 99999) . "." . $type; //图片名称
    $pic_url  = $path . $pic_name; //上传后图片路径+名称
    if (move_uploaded_file($name_tmp, $pic_url)) { //临时文件转移到目标文件夹
        echo json_encode(array("error" => "0", "pic" => $pic_url, "name" => $pic_name));
    } else {
        echo json_encode(array("error" => "上传有误,清检查服务器配置!"));
    }
}
?>

建立多个plupload

$("#v_m").click(function(){
        myselect=document.getElementById("v_m");
        index=myselect.selectedIndex ;
        option = myselect.options[index].text;
        upload_total = option;
        var html = "";
        for( var i = 0; i < option; i++ )
        {
            var j = i+1;
            html += "";
            html += "选项"+j+":"+"";
            html += "     ";
            html +=     "";
            html += "";
        }
        $("#nnnn").html(html);
        var ids = new Array("btn1","btn2","btn3","btn4","btn5","btn6","btn7","btn8","btn9","btn10");
        $.each(ids,function(i,n){
            var self = this.toString();
            //实例化一个plupload上传对象
            var uploader = new plupload.Uploader({
                browse_button : self, //触发文件选择对话框的按钮,为那个元素id
                url : "upload.php",//服务器端的上传页面地址
                max_file_size: '2mb',//限制为2MB
                filters: [{title: "Image files",extensions: "jpg,gif,png"}]//图片限制
            });
            //在实例对象上调用init()方法进行初始化
            uploader.init();
            //绑定各种事件,并在事件监听函数中做你想做的事
            uploader.bind('FilesAdded',function(uploader,files){
                uploader.start();
            });
            uploader.bind('FileUploaded',function(uploader,file,info){
                var data = eval("(" + info.response + ")");//解析返回的json数据
                $("#b"+self).attr('src',data.pic);
            });
        });

建立一个表格里面可以持续添加图片

下面是样式:

    
添加图片

本地上传

0张,还能上传9

下面是plupload实例化:

var upload_total = 9;//最多上传数量
    var uploader = new plupload.Uploader({//创建实例的构造方法
        runtimes: 'gears,html5,html4,silverlight,flash', //上传插件初始化选用那种方式的优先级顺序
        browse_button: ['btn', 'btn2'], // 上传按钮
        url: "upload.php", //远程上传地址
        flash_swf_url: 'public/static/plupload/Moxie.swf', //flash文件地址
        silverlight_xap_url: 'public/static/plupload/Moxie.xap', //silverlight文件地址
        filters: {
            max_file_size: '5mb', //最大上传文件大小(格式100b, 10kb, 10mb, 1gb)
            mime_types: [//允许文件上传类型
                {title: "files", extensions: "jpg,png,gif,jpeg"}
            ]
        },
        multi_selection: true, //true:ctrl多文件上传, false 单文件上传
        init: {
            FilesAdded: function(up, files) { //文件上传前
                var length_has_upload = $("#ul_pics").children("li").length;
                if (files.length >= upload_total) { //超过上传总数量则隐藏
                    $("#local_upload").hide();
                }
                var li = '';
                plupload.each(files, function(file) { //遍历文件
                    if (length_has_upload <= upload_total) {
                        li += "
  • 0%
  • "; } length_has_upload++; }); $("#ul_pics").prepend(li); uploader.start(); }, UploadProgress: function(up, file) { //上传中,显示进度条 var percent = file.percent; $("#" + file.id).find('.bar').css({"width": percent + "%"}); $("#" + file.id).find(".percent").text(percent + "%"); }, FileUploaded: function(up, file, info) { //文件上传成功的时候触发 showPhotoUploadBox($('#btn')); var uploaded_length = $(".img_common").length; if (uploaded_length <= upload_total) { var data = eval("(" + info.response + ")");//解析返回的json数据 $("#" + file.id).html("\n\ "); } showUploadBtn(); $(".photo_upload_close").hide(); }, Error: function(up, err) { //上传出错的时候触发 alert(err.message); } } }); uploader.init(); function delPic(pic, file_id) { //删除图片 参数1图片路径 参数2 随机数 $.post("deletePic.php", {pic: pic}, function(data) { $("#" + file_id).remove(); showUploadBtn(); }) } function showUploadBtn() { //是否显示上传按钮 var uploaded_length = $(".img_common").length; $("#uploaded_length").text(uploaded_length); var other_length = (upload_total - uploaded_length) > 0 ? upload_total - uploaded_length : 0; $("#upload_other").text(other_length); var uploaded_length = $(".img_common").length; if (uploaded_length >= upload_total) { $("#local_upload").hide(); } else { $("#local_upload").show(); } if(uploaded_length==0){ $(".photo_upload_close").show();} } function showPhotoUploadBox(obj) { //显示上传弹出层 var left = obj.offset().left; var top = obj.offset().top + 26; $("#photo_upload_box_outside").css({"left": left, "top": top}).show() } function photo_upload_close() { //关闭删除 $("#photo_upload_box_outside").fadeOut(500, function() { $(".li_upload").remove(); }) }

     

    你可能感兴趣的:(关于plupload的使用(代码经过测试可以使用))