upload.html 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <title>upload file</title>
  6. <link type="text/css" href="./bootstrap.min.css" rel="stylesheet" />
  7. <link type="text/css" href="./webuploader.css" rel="stylesheet" />
  8. </head>
  9. <body >
  10. <div id="uploader" class="wu-example" >
  11. <!--用来存放文件信息-->
  12. <div id="thelist" class="uploader-list" style="width:500px;"></div>
  13. <div class="btns">
  14. <div id="picker">选择文件</div>
  15. </div>
  16. </div>
  17. <div id="list">
  18. </div>
  19. <script src="./jquery-3.1.1.min.js"></script>
  20. <script src="./md5.js"></script>
  21. <script src="./webuploader.html5only.min.js"></script>
  22. <script type="text/javascript">
  23. var md5File;
  24. //监听分块上传过程中的时间点
  25. WebUploader.Uploader.register({
  26. "before-send-file":"beforeSendFile", // 整个文件上传前
  27. "before-send":"beforeSend", // 每个分片上传前
  28. "after-send-file": "afterSendFile" // 分片上传完毕
  29. },{
  30. //时间点1:所有分块进行上传之前调用此函数 ,检查文件存不存在
  31. beforeSendFile:function(file){
  32. var deferred = WebUploader.Deferred();
  33. md5File = hex_md5(file.name+file.size);//根据文件名称,大小确定文件唯一标记,这种方式不赞成使用
  34. $.ajax({
  35. type:"POST",
  36. url:"/bigfile/check",
  37. data:{
  38. md5File: md5File, //文件唯一标记
  39. },
  40. async: false, // 同步
  41. dataType:"json",
  42. success:function(response){
  43. if(response.resultCode == 1){ //文件存在,跳过 ,提示文件存在
  44. $('#' + file.id).find('p.state').text("file exist");
  45. $("#picker").show();
  46. }else{
  47. deferred.resolve(); //文件不存在或不完整,发送该文件
  48. }
  49. }
  50. } , function (jqXHR, textStatus, errorThrown) { //任何形式的验证失败,都触发重新上传
  51. deferred.resolve();
  52. } );
  53. return deferred.promise();
  54. },
  55. //时间点2:如果有分块上传,则每个分块上传之前调用此函数 ,判断分块存不存在
  56. beforeSend:function(block){
  57. var deferred = WebUploader.Deferred();
  58. $.ajax({
  59. type:"POST",
  60. url:"/bigfile/checkChunk",
  61. data:{
  62. md5File: md5File, //文件唯一标记
  63. chunk:block.chunk, //当前分块下标
  64. },
  65. dataType:"json",
  66. success:function(response){
  67. if(response.resultCode == 1){
  68. deferred.reject(); //分片存在,跳过
  69. }else{
  70. deferred.resolve(); //分块不存在或不完整,重新发送该分块内容
  71. }
  72. }
  73. }, function (jqXHR, textStatus, errorThrown) { //任何形式的验证失败,都触发重新上传
  74. deferred.resolve();
  75. });
  76. return deferred.promise();
  77. },
  78. //时间点3:分片上传完成后,通知后台合成分片
  79. afterSendFile: function (file) {
  80. var chunksTotal = Math.ceil(file.size / (5*1024*1024));
  81. if (chunksTotal >= 1) {
  82. //合并请求
  83. var deferred = WebUploader.Deferred();
  84. $.ajax({
  85. type: "POST",
  86. url: "/bigfile/merge",
  87. data: {
  88. name: file.name,
  89. md5File: md5File,
  90. chunks: chunksTotal
  91. },
  92. cache: false,
  93. async: false, // 同步
  94. dataType: "json",
  95. success:function(response){
  96. if(response.resultCode == 1){
  97. $('#' + file.id).find('p.state').text('upload success');
  98. $('#' + file.id).find('.progress').fadeOut();
  99. }else{
  100. $('#' + file.id).find('p.state').text('merge error');
  101. deferred.reject();
  102. }
  103. $("#picker").show();
  104. fileList();
  105. }
  106. })
  107. return deferred.promise();
  108. }
  109. }
  110. });
  111. var uploader = WebUploader.create({
  112. auto: true,// 选完文件后,是否自动上传。
  113. swf: '../static/Uploader.swf',// swf文件路径
  114. server: '/bigfile/upload',// 文件接收服务端。
  115. pick: '#picker',// 选择文件的按钮。可选。
  116. chunked:true,//开启分片上传
  117. chunkSize:5*1024*1024,//5M
  118. chunkRetry: 3,//错误重试次数
  119. prepareNextFile: true// 允许在文件传输时提前把下一个文件准备好
  120. });
  121. //上传添加参数
  122. uploader.on('uploadBeforeSend', function (obj, data, headers) {
  123. data.md5File=md5File;
  124. });
  125. // 当有文件被添加进队列的时候
  126. uploader.on( 'fileQueued', function( file ) {
  127. $("#picker").hide();//隐藏上传框
  128. $("#thelist").append( '<div id="' + file.id + '" class="item">' +
  129. '<h4 class="info">' + file.name + '</h4>' +
  130. '<p class="state"></p>' +
  131. '</div>' );
  132. });
  133. // 文件上传过程中创建进度条实时显示。
  134. uploader.on( 'uploadProgress', function( file, percentage ) {
  135. var $li = $( '#'+file.id ),
  136. $percent = $li.find('.progress .progress-bar');
  137. // 避免重复创建
  138. if ( !$percent.length ) {
  139. $percent = $('<div class="progress progress-striped active">' +
  140. '<div class="progress-bar" role="progressbar" style="width: 0%"></div>' +
  141. '</div>').appendTo( $li ).find('.progress-bar');
  142. }
  143. $li.find('p.state').text('Uploading');
  144. $percent.css( 'width', percentage * 100 + '%' );
  145. });
  146. fileList();
  147. function fileList() {
  148. $.ajax({
  149. type: "get",
  150. url: '/bigfile/getFiles',
  151. dataType: "json",
  152. success: function (data) {
  153. var listContent = "";
  154. $.each(data.mergeFiles, function (index, val) {
  155. if (val != null) {
  156. listContent += '<li><a href="/bigfile/down?path=' + val + '">' + val + '</a></li>';
  157. }
  158. $('#list').html(listContent);
  159. }
  160. );
  161. $.each(data.localFiles, function (index, val) {
  162. if (val != null) {
  163. listContent += '<li><a href="/bigfile/down?path=' + val + '">' + val + '</a></li>';
  164. }
  165. $('#list').html(listContent);
  166. }
  167. );
  168. },
  169. error: function (data) {
  170. console.log(data);
  171. }
  172. });
  173. }
  174. </script>
  175. </body>
  176. </html>