upfiles.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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,
  13. body {
  14. height: 100%;
  15. margin: 0;
  16. padding: 0;
  17. background: #fff;
  18. }
  19. ul {
  20. margin: 0;
  21. padding: 0;
  22. list-style: none;
  23. }
  24. #wrap {
  25. padding: 10px;
  26. }
  27. #topbar {
  28. padding: 10px 0;
  29. border-bottom: 1px solid #ccc;
  30. text-align: right;
  31. }
  32. #topbar button {
  33. margin: 0;
  34. margin-left: 5px;
  35. outline: none;
  36. padding: 4px 16px;
  37. box-sizing: border-box;
  38. display: inline-block;
  39. border: none;
  40. border-radius: 3px;
  41. text-align: center;
  42. cursor: pointer;
  43. font-size: 14px;
  44. line-height: 1.5;
  45. background-color: #f0f0f0;
  46. color: #223;
  47. }
  48. #topbar button.primary {
  49. background-color: #3d97d4;
  50. color: #fff;
  51. }
  52. #topbar button:hover {
  53. background-color: #207ab7;
  54. color: #fff;
  55. }
  56. #topbar button.removeall {
  57. float: left
  58. }
  59. #file_list {
  60. display: grid;
  61. grid-gap: 10px;
  62. grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  63. padding-top: 10px;
  64. }
  65. #file_list:empty:after {
  66. content: '可以直接拖拽文件到这里';
  67. color: #777;
  68. font-size: 0.8em;
  69. }
  70. #file_list li {
  71. position: relative;
  72. display: block;
  73. vertical-align: top;
  74. padding: 5px 5px;
  75. border-radius: 5px;
  76. }
  77. #file_list li.up-over {}
  78. #file_list li.up-now {}
  79. #file_list li.up-now:after {
  80. content: '';
  81. position: absolute;
  82. top: 0;
  83. left: 0;
  84. display: block;
  85. width: 100%;
  86. height: 100%;
  87. background: rgba(255, 255, 255, 0.8) url(loading.gif) center center no-repeat;
  88. border-radius: 5px;
  89. z-index: 999;
  90. }
  91. #file_list li:hover {
  92. background-color: #ddd;
  93. }
  94. #file_list li .picbox {
  95. display: flex;
  96. flex: 0 0 auto;
  97. justify-content: center;
  98. overflow: hidden;
  99. position: relative;
  100. width: 100%;
  101. padding-top: 100%;
  102. align-items: center;
  103. }
  104. #file_list li .picbox img {
  105. display: block;
  106. max-width: 100%;
  107. max-height: 100%;
  108. position: absolute;
  109. top: 50%;
  110. left: 50%;
  111. transform: translateX(-50%) translateY(-50%);
  112. }
  113. #file_list li.up-over .picbox:after {
  114. 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');
  115. position: absolute;
  116. bottom: 2px;
  117. right: 2px;
  118. z-index: 9;
  119. }
  120. #file_list li .tools {
  121. display: none;
  122. position: absolute;
  123. bottom: 5px;
  124. right: 5px;
  125. z-index: 99;
  126. }
  127. #file_list li:hover .tools {
  128. display: block;
  129. }
  130. #file_list li .tools .remove {
  131. cursor: pointer;
  132. }
  133. #file_list li .tools .remove:after {
  134. 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');
  135. }
  136. #file_list li .namebox {
  137. font-size: 14px;
  138. line-height: 20px;
  139. max-height: 40px;
  140. overflow: hidden;
  141. padding: 5px 10px;
  142. text-align: center;
  143. display: flex;
  144. justify-content: center;
  145. align-items: flex-start;
  146. }
  147. #file_list li .namebox span {
  148. word-break: break-all;
  149. vertical-align: top;
  150. }
  151. </style>
  152. </head>
  153. <body>
  154. <div id="wrap">
  155. <div id="topbar">
  156. <button class="addfile primary">+ 添加文件</button>
  157. <button class="upall primary">全部上传</button>
  158. <button class="removeall">清空列表</button>
  159. </div>
  160. <ul id="file_list"></ul>
  161. </div>
  162. <script>
  163. var editor = parent.tinymce.activeEditor;
  164. var axupimgs = parent.axupimgs;
  165. axupimgs.res = []; //存放本地文件的数组
  166. var blobInfo = { file: null }
  167. blobInfo.blob = function () {
  168. return this.file;
  169. }
  170. var upload_handler = axupimgs.images_upload_handler;
  171. var upload_base_path = axupimgs.images_upload_base_path;
  172. //为列表添加排序
  173. function reSort() {
  174. document.querySelectorAll('#file_list li').forEach((el, i) => {
  175. el.setAttribute('data-num', i);
  176. });
  177. }
  178. function addList(files) {
  179. var files_sum = files.length;
  180. var vDom = document.createDocumentFragment();
  181. for (let i = 0; i < files_sum; i++) {
  182. let file = files[i];
  183. let blobUrl = window.URL.createObjectURL(file)
  184. axupimgs.res.push({ file: file, blobUrl: blobUrl, url: '' });
  185. let li = document.createElement('li');
  186. li.setAttribute('class', 'up-no');
  187. li.setAttribute('data-time', file.lastModified);
  188. 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>';
  189. vDom.appendChild(li);
  190. }
  191. document.querySelector('#file_list').appendChild(vDom);
  192. //reSort();
  193. }
  194. //清空列表
  195. document.querySelector('#topbar .removeall').addEventListener('click', () => {
  196. axupimgs.res = []
  197. document.querySelectorAll('#file_list li').forEach((el, i) => {
  198. el.parentNode.removeChild(el)
  199. });
  200. });
  201. //拖拽添加
  202. document.addEventListener('dragover', (e) => {
  203. e.stopPropagation();
  204. e.preventDefault();
  205. e.dataTransfer.dropEffect = 'copy';
  206. });
  207. document.addEventListener('drop', (e) => {
  208. e.stopPropagation();
  209. e.preventDefault();
  210. if (!e.dataTransfer.files) { return false; }
  211. var dropfiles = e.dataTransfer.files;
  212. if (!(dropfiles.length > 0)) { return false; }
  213. var exts = axupimgs.axupimgs_filetype.replace(/(\s)+/g, '').toLowerCase().split(',');
  214. var files = [];
  215. for (let file of dropfiles) {
  216. ext = file.name.split('.');
  217. ext = '.' + ext[ext.length - 1];
  218. for (let s of exts) {
  219. if (s == ext) {
  220. files.push(file);
  221. break;
  222. }
  223. }
  224. }
  225. if (files.length > 0) {
  226. addList(files)
  227. }
  228. });
  229. //添加文件
  230. document.querySelector('#topbar .addfile').addEventListener('click', () => {
  231. var input = document.createElement('input');
  232. input.setAttribute('type', 'file');
  233. input.setAttribute('multiple', 'multiple');
  234. input.setAttribute('accept', axupimgs.axupimgs_filetype);
  235. input.click();
  236. input.onchange = function () {
  237. var files = this.files;
  238. addList(files);
  239. }
  240. });
  241. var file_i = 0;
  242. function upAllFiles(n) {//图片上传
  243. var len = axupimgs.res.length;
  244. file_i = n;
  245. if (len == n) {
  246. file_i = 0;
  247. document.querySelector('#topbar .upall').innerText = '全部上传';
  248. return true;
  249. }
  250. if (axupimgs.res[n].url != '') {
  251. n++;
  252. upAllFiles(n)
  253. } else {
  254. blobInfo.file = axupimgs.res[n].file;
  255. var reader = new FileReader();
  256. reader.readAsDataURL(blobInfo.file);
  257. reader.onload = function (e) {
  258. blobInfo.base64 = reader.result;
  259. upload_handler(blobInfo,
  260. function (url) {
  261. if (upload_base_path) {
  262. if (upload_base_path.slice(-1) == '/' && url.substr(0, 1) == '/') {
  263. url = upload_base_path + url.slice(1);
  264. } else if (upload_base_path.slice(-1) != '/' && url.substr(0, 1) != '/') {
  265. url = upload_base_path + '/' + url;
  266. } else {
  267. url = upload_base_path + url;
  268. }
  269. // alert(upload_base_path);
  270. }
  271. //alert(url)
  272. axupimgs.res[file_i].url = url;
  273. filename = url.split('/').pop();
  274. var li = document.querySelectorAll('#file_list li')[file_i];
  275. li.setAttribute('class', 'up-over');
  276. li.querySelector('.namebox span').innerText = filename;
  277. n++
  278. upAllFiles(n);
  279. }, function (err) {
  280. document.querySelector('#topbar .upall').innerText = '全部上传';
  281. document.querySelectorAll('#file_list li.up-now').forEach((el, i) => {
  282. el.setAttribute('class', 'up-no');
  283. });
  284. alert(err);
  285. });
  286. }
  287. }
  288. }
  289. document.querySelector('#topbar .upall').addEventListener('click', (e) => {
  290. if (e.target.innerText != '全部上传') {
  291. return false;
  292. }
  293. if (axupimgs.res.length > 0) {
  294. document.querySelectorAll('#file_list li.up-no').forEach((el, i) => {
  295. el.classList ? el.classList.add('up-now') : el.className += ' up-now';
  296. });
  297. e.target.innerText = '上传中...';
  298. upAllFiles(0);
  299. }
  300. });
  301. var observ_flist = new MutationObserver((muList, observe) => {
  302. if (muList[0].addedNodes.length > 0) {
  303. muList[0].addedNodes.forEach((el) => {
  304. el.querySelector('.remove').addEventListener('click', (e) => {
  305. var li = e.target.parentNode.parentNode;
  306. var n = li.getAttribute('data-num');
  307. var el = document.querySelectorAll('#file_list li')[n];
  308. el.parentNode.removeChild(el);
  309. axupimgs.res.splice(n, 1);
  310. });
  311. });
  312. }
  313. reSort();
  314. });
  315. observ_flist.observe(document.querySelector('#file_list'), { childList: true });
  316. </script>
  317. </body>
  318. </html>