uploadFile.html 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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: 3;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 :multiple="multiple" @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. multiple: true,
  27. aoption:{},
  28. },
  29. mounted() {
  30. console.log('加载webview');
  31. _this = this;
  32. this.files = new Map();
  33. document.addEventListener('plusready', (e)=>{
  34. let {debug,instantly,prohibited} = plus.webview.currentWebview();
  35. this.debug = debug;
  36. this.instantly = instantly;
  37. this.prohibited = prohibited;
  38. this.accept = prohibited.accept;
  39. this.multiple = prohibited.count > 1;
  40. this.aoption=this.option;
  41. location.href = 'callback?retype=updateOption';
  42. }, false);
  43. },
  44. methods: {
  45. toast(msg) {
  46. plus.nativeUI.toast(msg);
  47. },
  48. clear(name) {
  49. if (!name) {
  50. this.files.clear();
  51. return;
  52. }
  53. this.files.delete(name);
  54. },
  55. setData(option='{}') {
  56. this.debug&&console.log('更新参数:'+option);
  57. try{
  58. _this.option = JSON.parse(option);
  59. }catch(e){
  60. console.error('参数设置错误')
  61. }
  62. },
  63. async upload(name=''){
  64. if (name && this.files.has(name)) {
  65. await this.createUpload(this.files.get(name));
  66. }
  67. else {
  68. for (let item of this.files.values()) {
  69. if (item.type === 'waiting' || item.type === 'fail') {
  70. await this.createUpload(item);
  71. }
  72. }
  73. }
  74. },
  75. onChange(e) {
  76. let fileDom = this.$refs.file;
  77. for (let file of fileDom.files) {
  78. // if (this.files.size >= this.prohibited.count) {
  79. // this.toast(`只允许上传${this.prohibited.count}个文件`);
  80. // fileDom.value = '';
  81. // break;
  82. // }
  83. this.addFile(file);
  84. }
  85. this.uploadAfter();
  86. fileDom.value = '';
  87. },
  88. addFile(file) {
  89. if (file) {
  90. let name = file.name;
  91. this.debug&&console.log('文件名称',name,'大小',file.size);
  92. // 限制文件格式
  93. let suffix = name.substring(name.lastIndexOf(".")+1).toLowerCase();
  94. let formats = this.prohibited.formats.toLowerCase();
  95. if (formats&&!formats.includes(suffix)) {
  96. this.toast(`不支持上传${suffix.toUpperCase()}格式文件`);
  97. return;
  98. }
  99. // 限制文件大小
  100. if (file.size > 1024 * 1024 * Math.abs(this.prohibited.size)) {
  101. this.toast(`附件大小请勿超过${this.prohibited.size}M`)
  102. return;
  103. }
  104. let path = URL.createObjectURL(file);
  105. this.files.set(file.name,{file,path,name: file.name,size: file.size,progress: 0,type: 'waiting'});
  106. }
  107. },
  108. /**
  109. * @returns {Map} 已选择的文件Map集
  110. */
  111. callChange() {
  112. location.href = 'callback?retype=change&files=' + escape(JSON.stringify([...this.files]));
  113. },
  114. /**
  115. * @returns {object} 正在处理的当前对象
  116. */
  117. changeFilesItem(item,end='') {
  118. this.files.set(item.name,item);
  119. location.href = 'callback?retype=progress&end='+ end +'&item=' + escape(JSON.stringify(item));
  120. },
  121. uploadAfter() {
  122. this.callChange();
  123. this.instantly&&this.upload();
  124. },
  125. createUpload(item) {
  126. this.debug&&console.log('准备上传,option=:'+JSON.stringify(this.option));
  127. item.type = 'loading';
  128. delete item.responseText;
  129. return new Promise((resolve,reject)=>{
  130. let {url,name,method='POST',header={},formData={}} = this.aoption;
  131. let form = new FormData();
  132. for (let keys in formData) {
  133. form.append(keys, formData[keys])
  134. }
  135. form.append(name, item.file);
  136. let xmlRequest = new XMLHttpRequest();
  137. xmlRequest.open(method, url, true);
  138. for (let keys in header) {
  139. xmlRequest.setRequestHeader(keys, header[keys])
  140. }
  141. xmlRequest.upload.addEventListener(
  142. 'progress',
  143. event => {
  144. if (event.lengthComputable) {
  145. let progress = Math.ceil((event.loaded * 100) / event.total)
  146. if (progress <= 100) {
  147. item.progress = progress;
  148. this.changeFilesItem(item);
  149. }
  150. }
  151. },
  152. false
  153. );
  154. xmlRequest.ontimeout = () => {
  155. console.error('请求超时')
  156. item.type = 'fail';
  157. this.changeFilesItem(item,true);
  158. return resolve(false);
  159. }
  160. xmlRequest.onreadystatechange = ev => {
  161. if (xmlRequest.readyState == 4) {
  162. if (xmlRequest.status == 200) {
  163. this.debug && console.log('上传完成:' + xmlRequest.responseText)
  164. item['responseText'] = xmlRequest.responseText;
  165. item.type = 'success';
  166. this.changeFilesItem(item,true);
  167. return resolve(true);
  168. } else if (xmlRequest.status == 0) {
  169. console.error('status = 0 :请检查请求头Content-Type与服务端是否匹配,服务端已正确开启跨域,并且nginx未拦截阻止请求')
  170. }
  171. console.error('--ERROR--:status = ' + xmlRequest.status)
  172. item.type = 'fail';
  173. this.changeFilesItem(item,true);
  174. return resolve(false);
  175. }
  176. }
  177. xmlRequest.send(form)
  178. });
  179. }
  180. }
  181. });
  182. </script>
  183. </body>
  184. </html>