首先如何导入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();
})
}