No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

180 líneas
5.4 KiB

  1. <!DOCTYPE html>
  2. <html lang="zh-cn">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title class="title">[文件管理器]</title>
  6. <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
  7. <style type="text/css">
  8. .content {background: transparent;}
  9. .btn {position: relative;top: 0;left: 0;bottom: 0;right: 0;}
  10. .btn .file {position: fixed;z-index: 93;left: 0;right: 0;top: 0;bottom: 0;width: 100%;opacity: 0;}
  11. </style>
  12. </head>
  13. <body>
  14. <div id="content" class="content">
  15. <div class="btn">
  16. <input @change="onChange" :accept="accept" ref="file" class="file" type="file" />
  17. </div>
  18. </div>
  19. <script type="text/javascript" src="js/vue.min.js"></script>
  20. <script type="text/javascript">
  21. let _this;
  22. var vm = new Vue({
  23. el: '#content',
  24. data: {
  25. accept: '',
  26. },
  27. mounted() {
  28. console.log('加载webview');
  29. _this = this;
  30. this.files = new Map();
  31. document.addEventListener('plusready', (e)=>{
  32. let {debug,instantly,prohibited} = plus.webview.currentWebview();
  33. this.debug = debug;
  34. this.instantly = instantly;
  35. this.prohibited = prohibited;
  36. this.accept = prohibited.accept;
  37. location.href = 'callback?retype=updateOption';
  38. }, false);
  39. },
  40. methods: {
  41. toast(msg) {
  42. plus.nativeUI.toast(msg);
  43. },
  44. clear(name) {
  45. if (!name) {
  46. this.files.clear();
  47. return;
  48. }
  49. this.files.delete(name);
  50. },
  51. setData(option='{}') {
  52. this.debug&&console.log('更新参数:'+option);
  53. try{
  54. _this.option = JSON.parse(option);
  55. }catch(e){
  56. console.error('参数设置错误')
  57. }
  58. },
  59. async upload(name=''){
  60. if (name && this.files.has(name)) {
  61. await this.createUpload(this.files.get(name));
  62. }
  63. else {
  64. for (let item of this.files.values()) {
  65. if (item.type === 'waiting' || item.type === 'fail') {
  66. await this.createUpload(item);
  67. }
  68. }
  69. }
  70. },
  71. onChange(e) {
  72. let fileDom = this.$refs.file;
  73. let file = fileDom.files[0];
  74. let name = file.name;
  75. fileDom.value = '';
  76. this.debug&&console.log('文件名称',name,'大小',file.size);
  77. if (file) {
  78. // 限制文件格式
  79. let suffix = name.substring(name.lastIndexOf(".")+1).toLowerCase();
  80. let formats = this.prohibited.formats.toLowerCase();
  81. if (formats&&!formats.includes(suffix)) {
  82. this.toast(`不支持上传${suffix.toUpperCase()}格式文件`);
  83. return;
  84. }
  85. // 限制文件大小
  86. if (file.size > 1024 * 1024 * Math.abs(this.prohibited.size)) {
  87. this.toast(`附件大小请勿超过${this.prohibited.size}M`)
  88. return;
  89. }
  90. let path = URL.createObjectURL(file);
  91. this.files.set(file.name,{file,path,name: file.name,size: file.size,progress: 0,type: 'waiting'});
  92. this.callChange();
  93. this.instantly&&this.upload();
  94. }
  95. },
  96. /**
  97. * @returns {Map} 已选择的文件Map集
  98. */
  99. callChange() {
  100. location.href = 'callback?retype=change&files=' + escape(JSON.stringify([...this.files]));
  101. },
  102. /**
  103. * @returns {object} 正在处理的当前对象
  104. */
  105. changeFilesItem(item,end='') {
  106. this.files.set(item.name,item);
  107. location.href = 'callback?retype=progress&end='+ end +'&item=' + escape(JSON.stringify(item));
  108. },
  109. createUpload(item) {
  110. this.debug&&console.log('准备上传,option=:'+JSON.stringify(this.option));
  111. item.type = 'loading';
  112. delete item.responseText;
  113. return new Promise((resolve,reject)=>{
  114. let {url,name,method='POST',header={},formData={}} = this.option;
  115. let form = new FormData();
  116. for (let keys in formData) {
  117. form.append(keys, formData[keys])
  118. }
  119. form.append(name, item.file);
  120. let xmlRequest = new XMLHttpRequest();
  121. xmlRequest.open(method, url, true);
  122. for (let keys in header) {
  123. xmlRequest.setRequestHeader(keys, header[keys])
  124. }
  125. xmlRequest.upload.addEventListener(
  126. 'progress',
  127. event => {
  128. if (event.lengthComputable) {
  129. let progress = Math.ceil((event.loaded * 100) / event.total)
  130. if (progress <= 100) {
  131. item.progress = progress;
  132. this.changeFilesItem(item);
  133. }
  134. }
  135. },
  136. false
  137. );
  138. xmlRequest.ontimeout = () => {
  139. console.error('请求超时')
  140. item.type = 'fail';
  141. this.changeFilesItem(item,true);
  142. return resolve(false);
  143. }
  144. xmlRequest.onreadystatechange = ev => {
  145. if (xmlRequest.readyState == 4) {
  146. if (xmlRequest.status == 200) {
  147. this.debug && console.log('上传完成:' + xmlRequest.responseText)
  148. item['responseText'] = xmlRequest.responseText;
  149. item.type = 'success';
  150. this.changeFilesItem(item,true);
  151. return resolve(true);
  152. } else if (xmlRequest.status == 0) {
  153. console.error('status = 0 :请检查请求头Content-Type与服务端是否匹配,服务端已正确开启跨域,并且nginx未拦截阻止请求')
  154. }
  155. console.error('--ERROR--:status = ' + xmlRequest.status)
  156. item.type = 'fail';
  157. this.changeFilesItem(item,true);
  158. return resolve(false);
  159. }
  160. }
  161. xmlRequest.send(form)
  162. });
  163. }
  164. }
  165. });
  166. </script>
  167. </body>
  168. </html>