webupload 上传插件 完美版 - 拿走直接用

由于最近,我们产品web端口 ,涉及批量图片上传(你懂得, 商品嘛,都玩吐了),然后自己写了一个半成品,后来

各种体验问题,然后一想那么不是傻么? 这么多上传插件。


然后,那就烂大街的  webupload 呗,虽说我是 百度黑,不过百度确实有那么几个良心插件还是不错的 。


现在入正题,由于 webupload   功能强大,配置繁多,不过相对还是很好应用,不过遇到的问题也不少,供大家参考 

,最好只支持IE8以上,不然自己找虐,别找我额!敲打


直接上效果 上代码吧,里面也都有注释,你们自己理解下载就行了,只需要配合 api 和我的代码 改相应的 css 和上传地址 就可以无缝对接


HTML: 


  1. <div id="uploader-demo" class="controls">
  2. <div class="uploader-list fileList"> </div>
  3. <div class="filePicker">添加商品图册
  4. <br>最多 5 张
  5. </div>
  6. </div>



--------------------------------------------------------------------------------------------------------------------------------------------------------------------


css:


  
  
  1. .webuploader-container {
  2. position: relative;
  3. float: left;
  4. }
  5. .webuploader-element-invisible {
  6. position: absolute !important;
  7. clip: rect(1px 1px 1px 1px);
  8. /* IE6, IE7 */
  9. clip: rect(1px, 1px, 1px, 1px);
  10. }
  11. .webuploader-pick {
  12. display: inline-block;
  13. cursor: pointer;
  14. background: #fafafa;
  15. padding: 10px 15px;
  16. color: #666;
  17. text-align: center;
  18. border-radius: 3px;
  19. overflow: hidden;
  20. float: left;
  21. width: 128px;
  22. height: 88px;
  23. position: relative;
  24. border: 1px solid #d6dee3;
  25. overflow: hidden;
  26. padding-top: 60px;
  27. }
  28. .webuploader-pick-hover {
  29. background: #00A3C6;
  30. color: #fff;
  31. }
  32. .webuploader-pick-disable {
  33. opacity: 0.6;
  34. pointer-events: none;
  35. }
  36. /*demo样式*/
  37. #picker {
  38. display: inline-block;
  39. line-height: 1.428571429;
  40. vertical-align: middle;
  41. margin: 0 12px 0 0;
  42. }
  43. #picker .webuploader-pick {
  44. padding: 6px 12px;
  45. display: block;
  46. }
  47. #uploader-demo .thumbnail {
  48. width: 150px;
  49. height: 150px;
  50. overflow: hidden;
  51. }
  52. #uploader-demo .thumbnail img {
  53. width: 100%;
  54. }
  55. .uploader-list {
  56. /*width: 100%;*/
  57. overflow: hidden;
  58. float: left;
  59. }
  60. .file-item {
  61. float: left;
  62. position: relative;
  63. margin: 0 20px 20px 0;
  64. padding: 5px;
  65. }
  66. .file-item .error {
  67. position: absolute;
  68. top: 4px;
  69. left: 4px;
  70. right: 4px;
  71. background: red;
  72. color: white;
  73. text-align: center;
  74. height: 20px;
  75. font-size: 14px;
  76. line-height: 23px;
  77. }
  78. .file-item .info {
  79. position: absolute;
  80. left: 4px;
  81. bottom: 4px;
  82. right: 4px;
  83. height: 20px;
  84. line-height: 20px;
  85. text-indent: 5px;
  86. background: rgba(0, 0, 0, 0.6);
  87. color: white;
  88. overflow: hidden;
  89. white-space: nowrap;
  90. text-overflow: ellipsis;
  91. font-size: 12px;
  92. z-index: 10;
  93. }
  94. /* 上次成功*/
  95. .upload-state-done:after {
  96. content: "\f00c";
  97. font-family: FontAwesome;
  98. font-style: normal;
  99. font-weight: normal;
  100. line-height: 1;
  101. -webkit-font-smoothing: antialiased;
  102. -moz-osx-font-smoothing: grayscale;
  103. font-size: 32px;
  104. position: absolute;
  105. bottom: 0;
  106. right: 4px;
  107. color: #4cae4c;
  108. z-index: 99;
  109. }
  110. /*进度条*/
  111. .file-item .progress {
  112. position: absolute;
  113. right: 4px;
  114. bottom: 4px;
  115. height: 3px;
  116. left: 4px;
  117. height: 4px;
  118. overflow: hidden;
  119. z-index: 15;
  120. margin: 0;
  121. padding: 0;
  122. border-radius: 0;
  123. background: transparent;
  124. }
  125. /*进度条*/
  126. .file-item .progress span {
  127. display: block;
  128. overflow: hidden;
  129. width: 0;
  130. height: 100%;
  131. background: #d14 url(images/progress.png) repeat-x;
  132. -webit-transition: width 200ms linear;
  133. -moz-transition: width 200ms linear;
  134. -o-transition: width 200ms linear;
  135. -ms-transition: width 200ms linear;
  136. transition: width 200ms linear;
  137. -webkit-animation: progressmove 2s linear infinite;
  138. -moz-animation: progressmove 2s linear infinite;
  139. -o-animation: progressmove 2s linear infinite;
  140. -ms-animation: progressmove 2s linear infinite;
  141. animation: progressmove 2s linear infinite;
  142. -webkit-transform: translateZ(0);
  143. }
  144. /*取消 删除*/
  145. .file-item div.file-panel {
  146. position: absolute;
  147. height: 0;
  148. filter: progid: DXImageTransform.Microsoft.gradient(GradientType=0, startColorstr='#80000000', endColorstr='#80000000')\0;
  149. background: rgba( 0, 0, 0, 0.6);
  150. width: 100%;
  151. bottom: 0;
  152. left: 0;
  153. overflow: hidden;
  154. z-index: 300;
  155. color: #fff;
  156. line-height: 30px;
  157. text-align: right;
  158. }
  159. .file-item div.file-panel span {
  160. width: 24px;
  161. height: 24px;
  162. display: inline;
  163. float: right;
  164. text-indent: -9999px;
  165. overflow: hidden;
  166. background: url(images/icons.png) no-repeat;
  167. margin: 5px 1px 1px;
  168. cursor: pointer;
  169. }
  170. .file-item div.file-panel span.rotateLeft {
  171. background-position: 0 -24px;
  172. }
  173. .file-item div.file-panel span.rotateLeft:hover {
  174. background-position: 0 0;
  175. }
  176. .file-item div.file-panel span.rotateRight {
  177. background-position: -24px -24px;
  178. }
  179. .file-item div.file-panel span.rotateRight:hover {
  180. background-position: -24px 0;
  181. }
  182. .file-item div.file-panel span.cancel {
  183. background-position: -48px -24px;
  184. }
  185. .file-item div.file-panel span.cancel:hover {
  186. background-position: -48px 0;
  187. }
  188. @-webkit-keyframes progressmove {
  189. 0% {
  190. background-position: 0 0;
  191. }
  192. 100% {
  193. background-position: 17px 0;
  194. }
  195. }
  196. @-moz-keyframes progressmove {
  197. 0% {
  198. background-position: 0 0;
  199. }
  200. 100% {
  201. background-position: 17px 0;
  202. }
  203. }
  204. @keyframes progressmove {
  205. 0% {
  206. background-position: 0 0;
  207. }
  208. 100% {
  209. background-position: 17px 0;
  210. }
  211. }
  212. a.travis {
  213. position: relative;
  214. top: -4px;
  215. right: 15px;
  216. }
----------------------------------------- ----------------------------------------- ----------------------------------------- -----------------------------------------


JS(重要部分,自己看 百度api 和注释)

  
  
  1. // 图片webupload 上传
  2. $(function() {
  3. var $ = jQuery,
  4. $list = $('.fileList'),
  5. $filePicker = $('.filePicker'), // 上传按钮
  6. $upimgmax = 5, // 支持上传最大个数
  7. // 优化retina, 在retina下这个值是2
  8. ratio = window.devicePixelRatio || 1,
  9. // 缩略图大小
  10. thumbnailWidth = 100 * ratio,
  11. thumbnailHeight = 100 * ratio,
  12. // 初始化Web Uploader
  13. uploader = WebUploader.create({
  14. // 自动上传。
  15. auto: true,
  16. // swf文件路径
  17. swf: 'webuploader/Uploader.swf',
  18. // 文件接收服务端。
  19. server: 你的后台图片上传接受地址 也就是uploadurl, // 这里是图片上传地址
  20. // 选择文件的按钮。可选。
  21. // 内部根据当前运行是创建,可能是input元素,也可能是flash
  22. pick: {
  23. id: $filePicker,
  24. // multiple: false
  25. },
  26. duplicate: true,
  27. fileSingleSizeLimit: 5242880, // 单个文件大小
  28. fileNumLimit: $upimgmax, // 限制上传个数
  29. accept: {
  30. title: 'Images',
  31. extensions: 'jpg,jpeg,png',
  32. mimeTypes: 'image/jpg,image/jpeg,image/png' //修改这行
  33. },
  34. thumb: {
  35. width: 110,
  36. height: 110,
  37. // 图片质量,只有type为`image/jpeg`的时候才有效。
  38. quality: 70,
  39. // 是否允许放大,如果想要生成小图的时候不失真,此选项应该设置为false.
  40. allowMagnify: true,
  41. // 是否允许裁剪。
  42. crop: true,
  43. // 为空的话则保留原有图片格式。
  44. // 否则强制转换成指定的类型。
  45. type: 'image/jpeg'
  46. }
  47. });
  48. // 当有文件添加进来的时候
  49. uploader.on('fileQueued', function(file) {
  50. var $li = $(
  51. '<div id="' + file.id + '" class="file-item thumbnail">' +
  52. '<img>' +
  53. // '<div class="info">' + file.name + '</div>' +
  54. '<div class = "file-panel" style = "height: 30px;" > 删除 ' +
  55. '<span class = "cancel delimgbtns" title="删除"></span></div>' +
  56. '</div>'
  57. ),
  58. $img = $li.find('img');
  59. $list.append($li);
  60. removefiles(file); // 文件删除
  61. // 创建缩略图
  62. uploader.makeThumb(file, function(error, src) {
  63. if (error) {
  64. $img.replaceWith('<span>不能预览</span>');
  65. return;
  66. }
  67. $img.attr('src', src);
  68. }, thumbnailWidth, thumbnailHeight);
  69. var uploadfilesNum = uploader.getStats().queueNum; // 共选中几个图片
  70. // 最多支持 5张
  71. if ($('.file-item').length >= $upimgmax) {
  72. $filePicker.hide();
  73. if ($('.file-item').length >= ($upimgmax + 1)) {
  74. // 中断 取消 大于 5张图片的对象
  75. uploader.removeFile(file, true);
  76. $('.file-item').last().remove();
  77. }
  78. } else {
  79. $filePicker.show();
  80. }
  81. });
  82. // 文件上传过程中创建进度条实时显示。
  83. uploader.on('uploadProgress', function(file, percentage) {
  84. var $li = $('#' + file.id),
  85. $percent = $li.find('.progress span');
  86. // 避免重复创建
  87. if (!$percent.length) {
  88. $percent = $('<p class="progress"><span></span></p>').appendTo($li).find('span');
  89. }
  90. $percent.css('width', percentage * 100 + '%');
  91. });
  92. // 文件上传成功,给item添加成功class, 用样式标记上传成功。
  93. uploader.on('uploadSuccess', function(file, response) {
  94. var $li = $('#' + file.id),
  95. $img = $li.find('img'),
  96. $fileUrl = response.data[0].url, // 图片上传地址
  97. $filename = file.name, // 上传文件名称
  98. $filesize = (file.size / 1024).toFixed(2); // 上传文件尺寸大小 保留2位
  99. $li.addClass('upload-state-done');
  100. // console.log(file);
  101. console.log(response);
  102. console.log('图片地址:' + $fileUrl);
  103. $img.attr('src', $fileUrl);
  104. removefiles(file); // 删除文件
  105. removehttpfiles();
  106. // 传值赋值
  107. // 商品详细图片 位置
  108. // 这里自己赋值 传值
  109. });
  110. // 文件上传失败,显示上传出错。
  111. uploader.on('uploadError', function(file) {
  112. var $li = $('#' + file.id),
  113. $error = $li.find('div.error');
  114. // 避免重复创建
  115. if (!$error.length) {
  116. $error = $('<div class="error"></div>').appendTo($li);
  117. }
  118. $error.text('上传失败');
  119. });
  120. // 完成上传完了,成功或者失败,先删除进度条。
  121. uploader.on('uploadComplete', function(file) {
  122. $('#' + file.id).find('.progress').remove();
  123. console.log(file);
  124. // 获取文件统计信息。返回一个包含一下信息的对象。
  125. /*successNum 上传成功的文件数
  126. progressNum 上传中的文件数
  127. cancelNum 被删除的文件数
  128. invalidNum 无效的文件数
  129. uploadFailNum 上传失败的文件数
  130. queueNum 还在队列中的文件数
  131. interruptNum 被暂停的文件数
  132. */
  133. // console.log(uploader.getStats().uploadFailNum);
  134. });
  135. uploader.on('error', function(handler) {
  136. if (handler == "Q_EXCEED_NUM_LIMIT") {
  137. layer.msg("最多只能上传 " + $upimgmax + "张图片");
  138. }
  139. });
  140. // 删除按钮事件
  141. function removefiles(file) {
  142. // 删除本条数据
  143. // $('.delimgbtns').each(function(index, el) {
  144. $('.delimgbtns').click(function() {
  145. // 中断 取消 传图
  146. uploader.removeFile(file, true);
  147. var spthisdiv = $(this).parent();
  148. spthisdiv.parent('.file-item').remove();
  149. $filePicker.show(); // 上传按钮显示
  150. });
  151. // });
  152. }
  153. // 删除服务器文件
  154. function removehttpfiles() {
  155. // 删除本条数据
  156. // $('.delimgbtns').each(function(index, el) {
  157. $('.delimgbtns').click(function() {
  158. // event.preventDefault();
  159. var spthisdiv = $(this).parent();
  160. // 如果上传成功才执行
  161. var thisimgsrc = spthisdiv.siblings('img').attr('src');
  162. var geturl = "attach/deleteFlowFile";
  163. $.axpost(
  164. geturl, {
  165. url: thisimgsrc,
  166. },
  167. //请求成功时处理
  168. function(data) {
  169. layer.msg('删除成功');
  170. });
  171. });
  172. // });
  173. }
  174. });
 
行了, 代码我都贴了再不给我点赞? 请不要做伸手党 ,如果有什么不明白 请 (看我安利我的 公众号)  不明白 或者直接加我  千人前端QQ群 : 147250970


扫描长按二维码,关注我的公众号



评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值