upfiles.html 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>axupimgs</title>
  6. <meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1.0, maximum-scale=1.0"/>
  7. <meta name="apple-mobile-web-app-capable" content="yes" />
  8. <meta name="apple-mobile-web-app-status-bar-style" content="black" />
  9. <meta name="format-detection" content="telephone=no">
  10. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
  11. <style>
  12. html,body{height:100%;margin:0;padding:0;background:#fff;}
  13. ul{margin:0;padding:0;list-style:none;}
  14. #wrap{padding:10px;}
  15. #topbar{padding:10px 0;border-bottom:1px solid #ccc;text-align:right;}
  16. #topbar button {margin:0;margin-left:5px;outline:none;padding: 4px 16px;box-sizing: border-box;
  17. display:inline-block;border:none;border-radius:3px;text-align:center;cursor:pointer;
  18. font-size:14px;line-height:1.5;background-color:#f0f0f0;color:#223;
  19. }
  20. #topbar button.primary{background-color:#3d97d4;color:#fff;}
  21. #topbar button:hover{background-color:#207ab7;color:#fff;}
  22. #topbar button.removeall{float:left}
  23. #file_list {display:grid;grid-gap:10px;grid-template-columns:repeat(auto-fill,minmax(160px,1fr));padding-top:10px;}
  24. #file_list:empty:after{content:'可以直接拖拽文件到这里';color:#777;font-size:0.8em;}
  25. #file_list li{position:relative;display:block;vertical-align:top;padding:5px 5px;border-radius:5px;}
  26. #file_list li.up-over {}
  27. #file_list li.up-now {}
  28. #file_list li.up-now:after{content:'';position:absolute;top:0;left:0;display:block;width:100%;height:100%;background:rgba(255,255,255,0.8) url(loading.gif) center center no-repeat;border-radius:5px;z-index:999;}
  29. #file_list li:hover{background-color:#ddd;}
  30. #file_list li .picbox {display:flex;flex:0 0 auto;justify-content:center;overflow:hidden;position:relative;width:100%;padding-top:100%;align-items:center;}
  31. #file_list li .picbox img {display:block;max-width:100%;max-height:100%;position:absolute;
  32. top:50%;left:50%;transform:translateX(-50%) translateY(-50%);}
  33. #file_list li.up-over .picbox:after{content:url('data:image/svg+xml;%20charset=utf8,%3Csvg%20viewBox%3D%220%200%201024%201024%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%2224%22%20height%3D%2224%22%3E%3Cpath%20d%3D%22M512%200C229.376%200%200%20229.376%200%20512s229.376%20512%20512%20512%20512-229.376%20512-512S794.624%200%20512%200z%22%20fill%3D%22%234AC711%22%3E%3C%2Fpath%3E%3Cpath%20d%3D%22M855.552%20394.752l-358.4%20358.4a50.9952%2050.9952%200%200%201-72.192%200l-204.8-204.8c-18.944-19.968-18.944-51.2%200-71.168a50.5344%2050.5344%200%200%201%2072.192-1.024L460.8%20644.608l322.048-322.048c19.968-18.944%2051.2-18.944%2071.168%200%2020.48%2019.456%2020.992%2051.712%201.536%2072.192z%22%20fill%3D%22%23FFFFFF%22%3E%3C%2Fpath%3E%3C%2Fsvg%3E');position:absolute;bottom:2px;right:2px;z-index:9;}
  34. #file_list li .tools {display:none;position:absolute;bottom:5px;right:5px;z-index:99;}
  35. #file_list li:hover .tools {display:block;}
  36. #file_list li .tools .remove{cursor:pointer;}
  37. #file_list li .tools .remove:after{content:url('data:image/svg+xml;%20charset=utf8,%3Csvg%20width=%2224%22%20height=%2224%22%20viewBox=%220%200%2024%2024%22%20xmlns=%22http://www.w3.org/2000/svg%22%3E%3Cpath%20d=%22M17%206h3a1%201%200%200%201%200%202h-1v11a3%203%200%200%201-3%203H8a3%203%200%200%201-3-3V8H4a1%201%200%201%201%200-2h3V5a3%203%200%200%201%203-3h4a3%203%200%200%201%203%203v1zm-2%200V5a1%201%200%200%200-1-1h-4a1%201%200%200%200-1%201v1h6zm2%202H7v11a1%201%200%200%200%201%201h8a1%201%200%200%200%201-1V8zm-8%203a1%201%200%200%201%202%200v6a1%201%200%200%201-2%200v-6zm4%200a1%201%200%200%201%202%200v6a1%201%200%200%201-2%200v-6z%22%3E%3C/path%3E%3C/svg%3E');}
  38. #file_list li .namebox {font-size:14px;line-height:20px;max-height:40px;overflow:hidden;padding:5px 10px;text-align:center;display:flex;justify-content:center;align-items:flex-start;}
  39. #file_list li .namebox span{word-break:break-all;vertical-align:top;}
  40. </style>
  41. </head>
  42. <body>
  43. <div id="wrap">
  44. <div id="topbar"><button class="addfile primary">+ 添加文件</button><button class="upall primary">全部上传</button><button class="removeall">清空列表</button></div>
  45. <ul id="file_list"></ul>
  46. </div>
  47. <script>
  48. var editor=parent.tinymce.activeEditor;
  49. var axupimgs=parent.axupimgs;
  50. axupimgs.res = []; //存放本地文件的数组
  51. var blobInfo = {file:null}
  52. blobInfo.blob = function(){return this.file;}
  53. var upload_handler = axupimgs.images_upload_handler;
  54. var upload_base_path = axupimgs.images_upload_base_path;
  55. //为列表添加排序
  56. function reSort(){
  57. document.querySelectorAll('#file_list li').forEach((el,i)=>{
  58. el.setAttribute('data-num',i);
  59. });
  60. }
  61. function addList(files){
  62. var files_sum = files.length;
  63. var vDom = document.createDocumentFragment();
  64. for(let i=0;i<files_sum;i++){
  65. let file = files[i];
  66. let blobUrl = window.URL.createObjectURL(file)
  67. axupimgs.res.push({file:file,blobUrl:blobUrl,url:''});
  68. let li = document.createElement('li');
  69. li.setAttribute('class','up-no');
  70. li.setAttribute('data-time',file.lastModified);
  71. li.innerHTML='<div class="picbox"><img src="'+blobUrl+'"></div><div class="namebox"><span>'+file.name+'</span></div><div class="tools"><a class="remove"></a></div>';
  72. vDom.appendChild(li);
  73. }
  74. document.querySelector('#file_list').appendChild(vDom);
  75. //reSort();
  76. }
  77. //清空列表
  78. document.querySelector('#topbar .removeall').addEventListener('click',()=>{
  79. axupimgs.res=[]
  80. document.querySelectorAll('#file_list li').forEach((el,i)=>{
  81. el.parentNode.removeChild(el)
  82. });
  83. });
  84. //拖拽添加
  85. document.addEventListener('dragover', (e)=>{
  86. e.stopPropagation();
  87. e.preventDefault();
  88. e.dataTransfer.dropEffect = 'copy';
  89. });
  90. document.addEventListener('drop', (e)=>{
  91. e.stopPropagation();
  92. e.preventDefault();
  93. if(!e.dataTransfer.files){return false;}
  94. var dropfiles = e.dataTransfer.files;
  95. if(!(dropfiles.length>0)){return false;}
  96. var exts=axupimgs.axupimgs_filetype.replace(/(\s)+/g,'').toLowerCase().split(',');
  97. var files=[];
  98. for( let file of dropfiles ){
  99. ext = file.name.split('.');
  100. ext = '.'+ext[ext.length-1];
  101. for(let s of exts){
  102. if(s==ext){
  103. files.push(file);
  104. break;
  105. }
  106. }
  107. }
  108. if(files.length>0){ addList(files) }
  109. });
  110. //添加文件
  111. document.querySelector('#topbar .addfile').addEventListener('click',()=>{
  112. var input = document.createElement('input');
  113. input.setAttribute('type', 'file');
  114. input.setAttribute('multiple', 'multiple');
  115. input.setAttribute('accept', axupimgs.axupimgs_filetype);
  116. input.click();
  117. input.onchange = function() {
  118. var files = this.files;
  119. addList(files);
  120. }
  121. });
  122. var file_i = 0;
  123. function upAllFiles(n){
  124. var len = axupimgs.res.length;
  125. file_i = n;
  126. if(len == n){
  127. file_i=0;
  128. document.querySelector('#topbar .upall').innerText='全部上传';
  129. return true;
  130. }
  131. if( axupimgs.res[n].url!='' ){
  132. n++;
  133. upAllFiles(n)
  134. }else{
  135. blobInfo.file=axupimgs.res[n].file;
  136. upload_handler(blobInfo,function(url){
  137. if(upload_base_path){
  138. if(upload_base_path.slice(-1)=='/' && url.substr(0,1)=='/' ){
  139. url = upload_base_path + url.slice(1);
  140. }else if(upload_base_path.slice(-1)!='/' && url.substr(0,1)!='/' ){
  141. url = upload_base_path + '/' + url;
  142. }else{
  143. url = upload_base_path + url;
  144. }
  145. }
  146. axupimgs.res[file_i].url = url;
  147. filename = url.split('/').pop();
  148. var li = document.querySelectorAll('#file_list li')[file_i];
  149. li.setAttribute('class','up-over');
  150. li.querySelector('.namebox span').innerText = filename;
  151. n++
  152. upAllFiles(n);
  153. },function(err){
  154. document.querySelector('#topbar .upall').innerText='全部上传';
  155. document.querySelectorAll('#file_list li.up-now').forEach((el,i)=>{
  156. el.setAttribute('class','up-no');
  157. });
  158. alert(err);
  159. });
  160. }
  161. }
  162. document.querySelector('#topbar .upall').addEventListener('click',(e)=>{
  163. if(e.target.innerText!='全部上传'){return false;}
  164. if(axupimgs.res.length>0){
  165. document.querySelectorAll('#file_list li.up-no').forEach((el,i)=>{
  166. el.classList ? el.classList.add('up-now') : el.className+=' up-now';
  167. });
  168. e.target.innerText='上传中...';
  169. upAllFiles(0);
  170. }
  171. });
  172. var observ_flist = new MutationObserver( (muList,observe)=>{
  173. if(muList[0].addedNodes.length>0){
  174. muList[0].addedNodes.forEach((el)=>{
  175. el.querySelector('.remove').addEventListener('click',(e)=>{
  176. var li = e.target.parentNode.parentNode;
  177. var n = li.getAttribute('data-num');
  178. var el = document.querySelectorAll('#file_list li')[n];
  179. el.parentNode.removeChild(el);
  180. axupimgs.res.splice(n,1);
  181. });
  182. });
  183. }
  184. reSort();
  185. });
  186. observ_flist.observe(document.querySelector('#file_list'),{childList:true});
  187. </script>
  188. </body>
  189. </html>