WebUploader文件图片上传插件的使用

最近在项目中用到了百度的文件图片上传插件WebUploader。分享给大家

需要在 https://0x9.me/q2aln 点击打开链接下载

         // 初始化Web Uploader***上传图片
    var uploader = WebUploader.create({
        // 选完文件后,是否自动上传。
        auto: true,
        // 文件接收服务端地址,自动生成缩略图需要后端完成。
        server: '/common/uploadFile?imageZip=1',
        // 选择文件的按钮。可选。
        // 内部根据当前运行是创建,可能是input元素,也可能是flash.
        pick: '#sendimg',
        fileNumLimit: 5,
        //allowMagnify: false,
        // 只允许选择图片文件。
        accept:{
            title: 'Images',
            extensions: 'gif,jpg,jpeg,bmp,png',
            mimeTypes: 'image/*'
        }
    });
    // 当有文件添加进来的时候
        uploader.on( 'fileQueued', function( file ) {
            var $li = $(
                    '<div style="float:right" id="' + file.id + '" class="delimg">' +
                        '<img class="addimg"><div class="closeimg">×</div>' +
                    '</div>'
                    ),
            $img = $li.find('img');
        
            // $list为容器jQuery实例
            $("#piccon").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 );
            $li.on('click', function() {
                $(this).remove();
            })
        });
        // 文件上传过程中创建进度条实时显示。
            uploader.on( 'uploadProgress', function( file, percentage ) {
                var $li = $( '#'+file.id ),
                    $percent = $li.find('.progress span');
            
                // 避免重复创建
                if ( !$percent.length ) {
                    $percent = $('<p class="progress"><span></span></p>')
                            .appendTo( $li )
                            .find('span');
                }
            
                $percent.css( 'width', percentage * 100 + '%' );
            });
            
            // 文件上传成功,给item添加成功class, 用样式标记上传成功。
            uploader.on( 'uploadSuccess', function( file,response ) {
                imgurl=response.fileName;//这里可以拿到后台返回的图片名称
                //alert(imgurl);
                $( '#'+file.id ).addClass('upload-state-done');
            });
            
            // 文件上传失败,显示上传出错。
            uploader.on( 'uploadError', function( file ) {
                var $li = $( '#'+file.id ),
                    $error = $li.find('div.error');
            
                // 避免重复创建
                if ( !$error.length ) {
                    $error = $('<div class="error"></div>').appendTo( $li );
                }
            
                $error.text('上传失败');
            });
            
            // 完成上传完了,成功或者失败,先删除进度条。
            uploader.on( 'uploadComplete', function( file ) {
                $( '#'+file.id ).find('.progress').remove();
            });


使用webupload封装后的图片上传

     //图片上传初始化
   function webuploader(){
           var showimg=seturl("/dream/app/upload/");
        // 初始化Web Uploader***上传图片
        var uploader = WebUploader.create({
            // 选完文件后,是否自动上传。
            auto: true,
            // 文件接收服务端。
            server: seturl("/dream/app/fileupload.php"),
            // 选择文件的按钮。可选。
            // 内部根据当前运行是创建,可能是input元素,也可能是flash.
            pick: '#sendimg',
            fileNumLimit: 3,
            method:"POST",
            //allowMagnify: false,
            // 只允许选择图片文件。
            accept:{
                title: 'Images',
                extensions: 'gif,jpg,jpeg,png',
                mimeTypes: 'image/*'
            }
        });
        // 当有文件添加进来的时候
        uploader.on( 'fileQueued', function( file ) {
 
        });
        // 文件上传过程中创建进度条实时显示。
        uploader.on( 'uploadProgress', function( file, percentage ) {
            
        });
        
        // 文件上传成功,给item添加成功class, 用样式标记上传成功。
        uploader.on( 'uploadSuccess', function( file,response ) {
            $("#piccon").append('<img id="'+response._raw+'" src="'+showimg+response._raw+'" class="addimg"><span id="'+response._raw+'" class="cimg">×</span>');
            $( '#'+file.id ).addClass('upload-state-done');
        });
        
        // 文件上传失败,显示上传出错。
        uploader.on( 'uploadError', function( file ) {
            var $li = $( '#'+file.id ),
                $error = $li.find('div.error');
        
            // 避免重复创建
            if ( !$error.length ) {
                $error = $('<div class="error"></div>').appendTo( $li );
            }
        
            $error.text('上传失败');
        });
        
        // 完成上传完了,成功或者失败,先删除进度条。
        uploader.on( 'uploadComplete', function( file ) {
            $( '#'+file.id ).find('.progress').remove();
            $(".cimg").click(function(){
                $(this).prev("img").remove();
                $(this).remove();
            })
        });
   }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值