webuploader 小demo

页面写法javascript

<!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   我也作了一点点修改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框架的上传方法html

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)); } }
相关文章
相关标签/搜索