uploadFile.html 5.8 KB

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