pick: { id: '#filePicker', multiple:false, label: '点击选择图片' },
转自或参考:WebUploader 设置单个图片上传
https://blog.csdn.net/tuchui88/article/details/77870763javascript
咱们能够看到multiple就是咱们想要的 设为false就可,同时将fileNumLimit设为1,具体以下css
pick: { id: '#filePicker', multiple:false, label: '点击选择图片' },
fileNumLimit: 1,
这是在webuploader实例化时设置,其中id 是图片div 的id。html
<div id="uploader-demo">
<!--用来存放item-->
<div id="fileList" class="uploader-list"></div>
<div id="filePicker">选择图片</div>
</div>
Javas
1 {{--初始参数--}} 2 <script> 3 window.uploader_input='u_picture'; 4 </script> 5 6 <!--引入CSS--> 7 <link rel="stylesheet" type="text/css" href="/webuploader-0.1.5/webuploader.css"> 8 <!--引入JS--> 9 <script type="text/javascript" src="/webuploader-0.1.5/webuploader.js"></script> 10 11 <style> 12 .fry_file_uploader{ 13 text-align: center; 14 border: none; 15 border-bottom: 1px solid #ddd; 16 /*border-top: 1px solid #ddd;*/ 17 /*border-radius: 10px;*/ 18 margin-bottom: 10px; 19 } 20 .fry_file_uploader_img{ 21 width: 150px; 22 height: 150px; 23 } 24 .webuploader-pick{ 25 padding: 5px 10px; 26 } 27 </style> 28 29 30 31 {{--<!--dom结构部分-->--}} 32 {{--<div id="uploader-demo">--}} 33 {{--<!--用来存放item-->--}} 34 {{--<div id="fileList" class="uploader-list"></div>--}} 35 {{--<div id="filePicker">选择图片</div>--}} 36 {{--</div>--}} 37 38 39 <script> 40 // 图片上传demo 41 jQuery(function() { 42 var $ = jQuery, 43 $list = $('#fileList'), 44 // 优化retina, 在retina下这个值是2 45 ratio = window.devicePixelRatio || 1, 46 47 // 缩略图大小 48 thumbnailWidth = 100 * ratio, 49 thumbnailHeight = 100 * ratio, 50 51 // Web Uploader实例 52 uploader; 53 54 // 初始化Web Uploader 55 uploader = WebUploader.create({ 56 57 // 自动上传。 58 auto: true, 59 60 // swf文件路径 61 swf: '/webuploader-0.1.5/Uploader.swf', 62 63 formData: { 64 // 这里的token是外部生成的长期有效的,若是把token写死,是能够上传的。 65 _token:'{{csrf_token()}}' 66 }, 67 68 // 文件接收服务端。 69 server: '/component/uploader', 70 71 // 选择文件的按钮。可选。 72 // 内部根据当前运行是建立,多是input元素,也多是flash. 73 74 pick: { 75 id: '#filePicker', 76 multiple:false, 77 label: '选择图片' 78 }, 79 // fileNumLimit: 1, 80 81 // 只容许选择文件,可选。 82 accept: { 83 title: 'Images', 84 extensions: 'gif,jpg,jpeg,bmp,png', 85 mimeTypes: 'image/*' 86 } 87 }); 88 89 // 当有文件添加进来的时候 90 uploader.on( 'fileQueued', function( file ) { 91 var $li = $( 92 '<div id="' + file.id + '" class="fry_file_uploader file-item thumbnail">' + 93 '<img class="fry_file_uploader_img">' + 94 '<div class="info">' + file.name + '</div>' + 95 '</div>' 96 ), 97 $img = $li.find('img'); 98 $('#fileList').html(''); 99 $list.append( $li ); 100 101 // 建立缩略图 102 uploader.makeThumb( file, function( error, src ) { 103 if ( error ) { 104 $img.replaceWith('<span>不能预览</span>'); 105 return; 106 } 107 108 $img.attr( 'src', src ); 109 }, thumbnailWidth, thumbnailHeight ); 110 }); 111 112 // 文件上传过程当中建立进度条实时显示。 113 uploader.on( 'uploadProgress', function( file, percentage ) { 114 var $li = $( '#'+file.id ), 115 $percent = $li.find('.progress span'); 116 117 // 避免重复建立 118 if ( !$percent.length ) { 119 $percent = $('<p class="progress"><span></span></p>') 120 .appendTo( $li ) 121 .find('span'); 122 } 123 124 $percent.css( 'width', percentage * 100 + '%' ); 125 }); 126 127 // 文件上传成功,给item添加成功class, 用样式标记上传成功。 128 uploader.on( 'uploadSuccess', function( file,response ) { 129 130 //console.log(file); 131 $( '#'+file.id ).addClass('upload-state-done'); 132 //上传成功了 133 if(response.valid){ 134 $('#'+window.uploader_input).val(response.message); 135 layer_alert_fail_mobile('图片上传成功'); 136 }else{ 137 layer_alert_fail_mobile(response.message); 138 } 139 }); 140 141 // 文件上传失败,现实上传出错。 142 uploader.on( 'uploadError', function( file ) { 143 var $li = $( '#'+file.id ), 144 $error = $li.find('div.error'); 145 146 // 避免重复建立 147 if ( !$error.length ) { 148 $error = $('<div class="error"></div>').appendTo( $li ); 149 } 150 151 $error.text('上传失败'); 152 }); 153 154 // 完成上传完了,成功或者失败,先删除进度条。 155 uploader.on( 'uploadComplete', function( file ) { 156 $( '#'+file.id ).find('.progress').remove(); 157 }); 158 }); 159 </script>