农燊高科官方网站
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. jQuery(function () {
  2. var $ = jQuery,
  3. $list = $('#thelist'),
  4. $btn = $('#ctlBtn'),
  5. state = 'pending',
  6. uploader;
  7. uploader = WebUploader.create({
  8. // 不压缩image
  9. resize: false,
  10. // swf文件路径
  11. swf: 'js/Uploader.swf',
  12. // 文件接收服务端。
  13. server: 'http://webuploader.duapp.com/server/fileupload.php',
  14. // 选择文件的按钮。可选。
  15. // 内部根据当前运行是创建,可能是input元素,也可能是flash.
  16. pick: '#picker',
  17. // 只允许选择.xls,xlsx文件。
  18. accept: {
  19. title: 'Excel',
  20. extensions: 'xls,xlsx',
  21. mimeTypes: 'application/vnd.ms-excel'
  22. }
  23. });
  24. // 当有文件添加进来的时候
  25. uploader.on('fileQueued', function (file) {
  26. $list.append('<div id="' + file.id + '" class="item">' +
  27. '<h4 class="info">' + file.name + '</h4>' +
  28. '<p class="state">等待上传...</p>' +
  29. '</div>');
  30. });
  31. // 文件上传过程中创建进度条实时显示。
  32. uploader.on('uploadProgress', function (file, percentage) {
  33. var $li = $('#' + file.id),
  34. $percent = $li.find('.progress .progress-bar');
  35. // 避免重复创建
  36. if (!$percent.length) {
  37. $percent = $('<div class="progress progress-striped active">' +
  38. '<div class="progress-bar" role="progressbar" style="width: 0%">' +
  39. '</div>' +
  40. '</div>').appendTo($li).find('.progress-bar');
  41. }
  42. $li.find('p.state').text('上传中');
  43. $percent.css('width', percentage * 100 + '%');
  44. });
  45. uploader.on('uploadSuccess', function (file) {
  46. $('#' + file.id).find('p.state').text('已上传');
  47. });
  48. uploader.on('uploadError', function (file) {
  49. $('#' + file.id).find('p.state').text('上传出错');
  50. });
  51. uploader.on('uploadComplete', function (file) {
  52. $('#' + file.id).find('.progress').fadeOut();
  53. });
  54. uploader.on('all', function (type) {
  55. if (type === 'startUpload') {
  56. state = 'uploading';
  57. } else if (type === 'stopUpload') {
  58. state = 'paused';
  59. } else if (type === 'uploadFinished') {
  60. state = 'done';
  61. }
  62. if (state === 'uploading') {
  63. $btn.text('暂停上传');
  64. } else {
  65. $btn.text('开始上传');
  66. }
  67. });
  68. $btn.on('click', function () {
  69. if (state === 'uploading') {
  70. uploader.stop();
  71. } else {
  72. uploader.upload();
  73. }
  74. });
  75. });
  76. /*// 图片上传demo
  77. jQuery(function () {
  78. var $ = jQuery,
  79. $list = $('#fileList'),
  80. // 优化retina, 在retina下这个值是2
  81. ratio = window.devicePixelRatio || 1,
  82. // 缩略图大小
  83. thumbnailWidth = 100 * ratio,
  84. thumbnailHeight = 100 * ratio,
  85. // Web Uploader实例
  86. uploader;
  87. // 初始化Web Uploader
  88. uploader = WebUploader.create({
  89. // 自动上传。
  90. auto: true,
  91. // swf文件路径
  92. swf: '/js/Uploader.swf',
  93. // 文件接收服务端。
  94. server: 'http://webuploader.duapp.com/server/fileupload.php',
  95. // 选择文件的按钮。可选。
  96. // 内部根据当前运行是创建,可能是input元素,也可能是flash.
  97. pick: '#filePicker',
  98. // 只允许选择文件,可选。
  99. accept: {
  100. title: 'Images',
  101. extensions: 'gif,jpg,jpeg,bmp,png',
  102. mimeTypes: 'image/!*'
  103. }
  104. });
  105. // 当有文件添加进来的时候
  106. uploader.on('fileQueued', function (file) {
  107. var $li = $(
  108. '<div id="' + file.id + '" class="file-item thumbnail">' +
  109. '<img>' +
  110. '<div class="info">' + file.name + '</div>' +
  111. '</div>'
  112. ),
  113. $img = $li.find('img');
  114. $list.append($li);
  115. // 创建缩略图
  116. uploader.makeThumb(file, function (error, src) {
  117. if (error) {
  118. $img.replaceWith('<span>不能预览</span>');
  119. return;
  120. }
  121. $img.attr('src', src);
  122. }, thumbnailWidth, thumbnailHeight);
  123. });
  124. // 文件上传过程中创建进度条实时显示。
  125. uploader.on('uploadProgress', function (file, percentage) {
  126. var $li = $('#' + file.id),
  127. $percent = $li.find('.progress span');
  128. // 避免重复创建
  129. if (!$percent.length) {
  130. $percent = $('<p class="progress"><span></span></p>')
  131. .appendTo($li)
  132. .find('span');
  133. }
  134. $percent.css('width', percentage * 100 + '%');
  135. });
  136. // 文件上传成功,给item添加成功class, 用样式标记上传成功。
  137. uploader.on('uploadSuccess', function (file) {
  138. $('#' + file.id).addClass('upload-state-done');
  139. });
  140. // 文件上传失败,现实上传出错。
  141. uploader.on('uploadError', function (file) {
  142. var $li = $('#' + file.id),
  143. $error = $li.find('div.error');
  144. // 避免重复创建
  145. if (!$error.length) {
  146. $error = $('<div class="error"></div>').appendTo($li);
  147. }
  148. $error.text('上传失败');
  149. });
  150. // 完成上传完了,成功或者失败,先删除进度条。
  151. uploader.on('uploadComplete', function (file) {
  152. $('#' + file.id).find('.progress').remove();
  153. });
  154. });
  155. /*var $ = jQuery,
  156. $list = $('#thelist'),
  157. $btn = $('#ctlBtn'),
  158. state = 'pending';
  159. var uploader = WebUploader.create({
  160. // swf文件路径
  161. swf: 'js/Uploader.swf',
  162. // 文件接收服务端。
  163. server: 'http://webuploader.duapp.com/server/fileupload.php',
  164. // 选择文件的按钮。可选。
  165. // 内部根据当前运行是创建,可能是input元素,也可能是flash.
  166. pick: '#picker',
  167. // 不压缩image, 默认如果是jpeg,文件上传前会压缩一把再上传!
  168. resize: false
  169. });
  170. // 当有文件被添加进队列的时候
  171. uploader.on( 'fileQueued', function( file ) {
  172. $list.append( '<div id="' + file.id + '" class="item">' +
  173. '<h4 class="info">' + file.name + '</h4>' +
  174. '<p class="state">等待上传...</p>' +
  175. '</div>' );
  176. });
  177. // 文件上传过程中创建进度条实时显示。
  178. uploader.on( 'uploadProgress', function( file, percentage ) {
  179. var $li = $( '#'+file.id ),
  180. $percent = $li.find('.progress .progress-bar');
  181. // 避免重复创建
  182. if ( !$percent.length ) {
  183. $percent = $('<div class="progress progress-striped active">' +
  184. '<div class="progress-bar" role="progressbar" style="width: 0%">' +
  185. '</div>' +
  186. '</div>').appendTo( $li ).find('.progress-bar');
  187. }
  188. $li.find('p.state').text('上传中');
  189. $percent.css( 'width', percentage * 100 + '%' );
  190. });
  191. uploader.on( 'uploadSuccess', function( file ) {
  192. $( '#'+file.id ).find('p.state').text('已上传');
  193. });
  194. uploader.on( 'uploadError', function( file ) {
  195. $( '#'+file.id ).find('p.state').text('上传出错');
  196. });
  197. uploader.on( 'uploadComplete', function( file ) {
  198. $( '#'+file.id ).find('.progress').fadeOut();
  199. });*/