關於plupload的使用(程式碼經過測試可以使用)
阿新 • • 發佈:2018-10-31
首先如何匯入plupload
<script type="text/javascript" src="public/static/plupload/plupload.full.min.js"></script>
其中url引數:upload.php如下所示:
<?php $typeArr = array("jpg", "png", "gif", "jpeg"); //允許上傳檔案格式 $path = "public/images/upload/post/"; //上傳路徑 if (isset($_POST)) { $name = $_FILES['file']['name']; $size = $_FILES['file']['size']; $name_tmp = $_FILES['file']['tmp_name']; if (empty($name)) { echo json_encode(array("error" => "您還未選擇圖片")); 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 += "<tr >"; html += "<th style="+'width:150px;height:40px;font-size:16px;padding-left:10px;padding-right: 0px;'+">選項"+j+":"+"</th>"; html += "<td><a id="+'btn'+j+"><img style="+'width:100px;height:100px;'+" id="+'bbtn'+j+" src="+'public/images/local_upload.png'+"></a> </td>"; html += "<td><input name="+'neirong'+" type="+'text'+" style="+'width:350px;height:30px;font-size:16px'+" id="+'neirong'+"></td>"; html += "</tr>"; } $("#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); }); });
建立一個表格裡面可以持續新增圖片
下面是樣式:
<div class="send-action"> <span> <a id="btn" href="javascript:;"><i class="file image outline large icon"></i>新增圖片</a> </span> <div class="release"> <button class="ui teal button" onClick="saysub()">釋出 </button> </div> </div> </div> <div class="photo_upload_box_outside" id="photo_upload_box_outside" tabindex="2000"> <div class="photo_upload_box"> <a class="photo_upload_close" href="javascript:void(0);"onclick="photo_upload_close()"></a> <h1>本地上傳</h1> <p class="upload_num">共<span id="uploaded_length">0</span>張,還能上傳<span id="upload_other">9</span>張</p> <ul id="ul_pics" class="ul_pics clearfix"> <li id="local_upload"><img src="public/images/local_upload.png" id="btn2"/></li> </ul> <div class="arrow_layer"> <span class="arrow_top_area"><i class="arrow_top_bg"></i><em class="arrow_top"></em></span> </div> </div> </div>
下面是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 += "<li class='li_upload' id='" + file['id'] + "'><div class='progress'><span class='bar'></span><span class='percent'>0%</span></div></li>";
}
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("<input type='hidden'name='pic[]' value='" + data.pic + "'/><input type='hidden'name='pic_name[]' value='" + data.name + "'/>\n\
<img class='img_common' src='" + data.pic + "'/><span class='picbg'></span><a class='pic_close' onclick=delPic('" + data.pic + "','" + file.id + "')></a>");
}
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();
})
}