mywebuploader.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. jQuery(function() {
  2. var $this = $('.wu-example');
  3. $this.each(function(index, element) {
  4. var $ = jQuery,
  5. $list = $($(element).find('#thelist')),
  6. $btn = $($(element).find('#ctlBtn')),
  7. state = 'pending',
  8. uploader;
  9. // alert($btn.prop("outerHTML"));
  10. if($list.length<=0){
  11. return;
  12. }
  13. // var $totalSize = $list.attr('data-totalSize')|100;
  14. // var $singleSize = $list.attr('data-singleSize');
  15. // var $filenumlimit = $list.attr('data-filenumlimit');
  16. var fileName = $list.attr('data-filename');
  17. var $data_mimeTypes = $list.attr('data-mimeTypes');
  18. var $file_auto = $list.attr('data-auto');
  19. var $filenumlimit = $list.attr('data-filenumlimit');
  20. var $data_ext = $list.attr('data-ext')==''?'*':$list.attr('data-ext');
  21. var $type = $list.attr('data-mode');
  22. var $orgid = $list.attr('data-orgid');
  23. var auto = false;
  24. if($file_auto=='true'||$file_auto==true){
  25. auto = true;
  26. }
  27. uploader = WebUploader.create({
  28. auto:true,
  29. // fileSizeLimit: $totalSize,//限制上传所有文件大小
  30. // fileSingleSizeLimit: $singleSize,//限制上传单个文件大小
  31. // 不压缩image
  32. resize: false,
  33. fileNumLimit:$filenumlimit,
  34. // swf文件路径
  35. swf: '/public/static/webuploader/Uploader.swf',
  36. // 文件接收服务端。
  37. server: '/Upload/uploadfile',
  38. // 选择文件的按钮。可选。
  39. // 内部根据当前运行是创建,可能是input元素,也可能是flash.
  40. pick: '#picker'+fileName.replace('[]',''),
  41. formData:{
  42. guid: WebUploader.Base.guid(),
  43. ttype: $type,
  44. orgId: $orgid
  45. },
  46. fileVal:'file',
  47. chunked: false,
  48. chunkSize: 1 * 1024*1024,//1M 分1片
  49. threads: 4,
  50. accept: {
  51. extensions: $data_ext,
  52. mimeTypes: $data_mimeTypes
  53. }
  54. });
  55. // 当有文件添加进来的时候
  56. uploader.on( 'fileQueued', function( file ) {
  57. $list.append( '<tr id="' + file.id + '">' +
  58. '<td>' + file.name + '<input type="hidden" name='+fileName+'>'+'</td>' +
  59. '<td id="state">等待上传...</td>' +
  60. '<td id="fileProgress"><a href="javascript:void(0);" class="remove-this">删除</a></td>' +
  61. '</tr>' );
  62. //删除
  63. $list.on('click', '.remove-this', function () {
  64. if(file !=undefined){
  65. uploader.removeFile(file);
  66. }
  67. $(this).parent().parent().remove();
  68. });
  69. });
  70. // 文件上传过程中创建进度条实时显示。
  71. uploader.on( 'uploadProgress', function( file, percentage ) {
  72. var $li = $( '#'+file.id ),
  73. $percent = $li.find('.progress .progress-bar');
  74. var $licon = $li.find('#fileProgress');
  75. // 避免重复创建
  76. if ( !$percent.length ) {
  77. $licon.empty();
  78. $percent = $('<div class="progress progress-striped active">' +
  79. '<div class="progress-bar" role="progressbar" style="width: 0%">' +
  80. '</div>' +
  81. '</div>').appendTo($licon).find('.progress-bar');
  82. }
  83. $li.find('#state').text('上传中...');
  84. $percent.css( 'width', percentage * 100 + '%' );
  85. });
  86. uploader.on( 'uploadSuccess', function( file, response ) {
  87. var fileId = $( '#'+file.id );
  88. fileId.find('.progress').fadeOut();
  89. fileId.find('#fileProgress').html('<a href="javascript:void(0);" class="remove-this">删除</a>');
  90. // if (response.success) {
  91. // fileId.find('#state').html('<span class="text-success">已上传</span>');
  92. // $( '#'+file.id ).find('input').val(response.savePath);
  93. // }else{
  94. // fileId.find('#state').html('<span class="text-danger">上传出错</span>');
  95. // }
  96. if (response.hasError == false) {
  97. fileId.find('#state').html('<span class="text-success">已上传</span>');
  98. $( '#'+file.id ).find('input').val(response.savePath);
  99. }else{
  100. fileId.find('#state').html('<span class="text-danger">上传出错</span>');
  101. }
  102. });
  103. uploader.on( 'uploadError', function( file ) {
  104. var fileId = $( '#'+file.id );
  105. fileId.find('#state').html('<span class="text-danger">上传出错</span>');
  106. fileId.find('.progress').fadeOut();
  107. fileId.find('#fileProgress').html('<a href="javascript:void(0);" class="remove-this">删除</a>');
  108. });
  109. uploader.on( 'uploadComplete', function( file ) {
  110. $( '#'+file.id ).find('.progress').fadeOut();
  111. });
  112. uploader.on( 'all', function( type ) {
  113. if ( type === 'startUpload' ) {
  114. state = 'uploading';
  115. } else if ( type === 'stopUpload' ) {
  116. state = 'paused';
  117. } else if ( type === 'uploadFinished' ) {
  118. state = 'done';
  119. }
  120. if ( state === 'uploading' ) {
  121. $btn.text('暂停上传');
  122. } else {
  123. $btn.text('开始上传');
  124. }
  125. });
  126. $btn.on( 'click', function() {
  127. if ( state === 'uploading' ) {
  128. uploader.stop();
  129. } else {
  130. uploader.upload();
  131. }
  132. });
  133. });
  134. });
  135. //删除
  136. function delFile(obj,file) {
  137. obj.parent().parent().remove();
  138. }