页面写法

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>web uploader测试</title>
</head>
<body>
<script src="https://cdn.bootcss.com/jquery/1.11.0-rc1/jquery.js"></script>
<link rel="stylesheet" type="text/css" href="__PUBLIC__/webup/webuploader.css">
<script type="text/javascript" src="__PUBLIC__/webup/webuploader.js"></script> <div id="uploader-demo">
<!--用来存放item-->
<div id="fileList" class="uploader-list"></div>
<div id="filePicker">选择图片</div>
<div id="upstart">开始上传</div>
<!-- 下面是上传成功返回的图片路径,用户form提交用的 -->
<form action="{:U('index/shangchuan_save')}" method="post"> <div id="upok_result"></div>
<input type="submit" value="save" />
</form>
</div> <script>
var BASE_URL = "__PUBLIC__/webup";
var uploader = WebUploader.create({ // 选完文件后,是否自动上传。
auto: false, // swf文件路径
swf: BASE_URL + '/Uploader.swf', // 文件接收服务端。此为tp3框架的上传方法请求,根据需求和框架修改
server: '{:U("index/shangchuan_up")}', // 选择文件的按钮。可选。
// 内部根据当前运行是创建,可能是input元素,也可能是flash.
pick: '#filePicker', // 只允许选择图片文件。
accept: {
title: 'Images',
extensions: 'gif,jpg,jpeg,bmp,png',
mimeTypes: 'image/jpg,image/jpeg,image/png'
}
});
// 当有文件添加进来的时候
uploader.on( 'fileQueued', function( file ) {
var $li = $(
'<div id="' + file.id + '" class="file-item thumbnail">' +
'<span class="jieguo success">ok</span>'+
'<img class="web_up_img">' +
'<div class="info">' + file.name + '</div>' +
'</div>'
),
$img = $li.find('img'); // $list为容器jQuery实例
$('#fileList').append( $li ); // 创建缩略图
// 如果为非图片文件,可以不用调用此方法。
// thumbnailWidth x thumbnailHeight 为 100 x 100
uploader.makeThumb( file, function( error, src ) {
if ( error ) {
$img.replaceWith('<span>不能预览</span>');
return;
} $img.attr( 'src', src );
}, 100, 100 );
});
// 文件上传过程中创建进度条实时显示。
uploader.on( 'uploadProgress', function( file, percentage ) {
var $li = $( '#'+file.id ),
$percent = $li.find('.progress span');
// 避免重复创建
if ( !$percent.length ) {
$percent = $('<p class="progress" style="background-color:red;"><span></span></p>')
.appendTo( $li )
.find('span');
}
$percent.css( 'width', percentage * 100 + '%' );
}); // 文件上传成功,给item添加成功class, 用样式标记上传成功。
uploader.on( 'uploadSuccess', function( file,response ) {
if(response.status==1){
//增加这个样式,上传成功的提示就显示出来了
$( '#'+file.id ).addClass('upload-state-done');
//上传成功存到隐藏的div里面用于form提交
var str = "<input type='text' name='photos[]' value='"
+response.msg.file.savepath
+response.msg.file.savename
+"' id='res_"
+file.id
+"' />";
// console.log(str);
$('#upok_result').append(str);
}else{
//上传失败显示提示
$( '#'+file.id ).addClass('upload-state-done-error');
var $li = $( '#'+file.id );
$('<span class="jieguo error">上传失败</span>').prependTo( $li );
} }); // 文件上传失败,显示上传出错。
uploader.on( 'uploadError', function( file ) {
//上传失败显示提示
$( '#'+file.id ).addClass('upload-state-done-error');
var $li = $( '#'+file.id ),
$error = $li.find('span.error'); // 避免重复创建
if ( !$error.length ) {
$error = $('<span class="jieguo error">上传失败</span>').appendTo( $li );
} }); // 完成上传完了,成功或者失败,先删除进度条。
uploader.on( 'uploadComplete', function( file ) {
//删除进度条
$( '#'+file.id ).find('.progress').remove();
//增加删除按钮
$( '#'+file.id ).append('<span class="jieguo success" style="cursor:pointer;" onclick=\'delimg("'+file.id+'")\'>删除</span>');
});
$("#upstart").click(function(){
uploader.upload();
}); //zll 删除图片
function delimg(fileid){
$("#"+fileid).remove();
$("#res_"+fileid).remove();
}
</script>
</body>
</html>

webuploader.css   我也做了一点点修改

.webuploader-container {
position: relative;
float: left;
}
.webuploader-element-invisible {
position: absolute !important;
clip: rect(1px 1px 1px 1px); /* IE6, IE7 */
clip: rect(1px,1px,1px,1px);
}
.webuploader-pick {
position: relative;
display: inline-block;
cursor: pointer;
background: #00b7ee;
padding: 10px 15px;
color: #fff;
text-align: center;
border-radius: 3px;
overflow: hidden;
}
.webuploader-pick-hover {
background: #00a2d4;
} .webuploader-pick-disable {
opacity: 0.6;
pointer-events:none;
}
.uploader-list{
display: flex;
flex-wrap: wrap;
flex-direction: row;
width: 300px;
}
.file-item{
/*height: 150px;*/
width: 100px;
box-sizing: border-box;
margin:5px;
border: 1px solid gray;
padding: 5px;
box-shadow: 0 0 2px 1px grey;
}
.info{
color: white;
background-color: rgba(0,0,0,0.5);
line-height: 20px;
font-size: 12px;
text-align: center;
margin-top: -24px;
position: relative;
height: 20px;
}
.web_up_img{
width: 100%;
}
#upstart{
background-color: #669584;
color: white;
width: 94px;
height: 41px;
line-height: 41px;
text-align: center;
border-radius: 3px;
float: left;
margin-left: 10px;
}
.jieguo{
display: none;
color: white;
text-align: center;
font-size: 12px;
height: 14px;
line-height: 14px;
}
.success{
background-color: green;
}
.error{
background-color: red;
}
.upload-state-done .success{
display: block;
}
.upload-state-done-error .error{
display: block;
}

tp3框架的上传方法

public function shangchuan_up(){
$upload = new \Think\Upload();// 实例化上传类
$upload->maxSize = 3145728 ;// 设置附件上传大小
$upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
$upload->rootPath = './Uploads/'; // 设置附件上传根目录
$upload->savePath = ''; // 设置附件上传(子)目录
// 上传文件
$info = $upload->upload();
if(!$info) {// 上传错误提示错误信息
// $this->error($upload->getError());
$this->ajaxReturn(array('status'=>0,'msg'=>$upload->getError()));
}else{// 上传成功
$this->ajaxReturn(array('status'=>1,'msg'=>$info));
}
}

最新文章

  1. Harmonic Number(调和级数+欧拉常数)
  2. Quartz.net2.2初体验
  3. 修改Tomcat服务器的端口号
  4. dig 命令详解(转载) - 阿权的书房
  5. sublime 安装 Terminal 使用 cmder
  6. Angularjs1.2版本与1.3版本中控制器的问题
  7. MySQL数据表的创建、查看、插入
  8. RabbitMQ-客户端
  9. 【项目记录】-路灯监测 gmap.net
  10. [HAOI 2008]糖果传递
  11. 给一个Unix域套接字bind一个路径名
  12. ECC椭圆曲线以及计算出公钥的过程(BTC为例)
  13. NE76003单片机调试DS18B20 步骤
  14. [Hive_add_6] Hive 实现 Word Count
  15. go 方法
  16. redis:list列表类型的操作
  17. ListFiles():返回Files类型数组,可以用getName()来访问到文件名。
  18. Spring Boot features - Profiles
  19. Alpha 冲刺三
  20. container / pull-left

热门文章

  1. Python datetime time 等时间 日期 之间的计算和相互转化
  2. PHP防止Xss攻击
  3. 非对称算法,散列(Hash)以及证书的那些事
  4. 给Linux设置SSH登录邮件提醒
  5. Android学习笔记之详细讲解画圆角图片
  6. 《四》JAVA 字符输入输出流
  7. Direct2D开发:Direct2D 和 GDI 互操作性概述
  8. mysql中load data Infile运用
  9. JSON序列化和解析
  10. sql server中新增一条数据后返回该数据的ID