uploadFile.html 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. console.log('this.size',this.prohibited.size);
  86. // 限制文件大小
  87. if (file.size > 1024 * 1024 * Math.abs(this.prohibited.size)) {
  88. this.toast(`附件大小请勿超过${this.prohibited.size}M`)
  89. return;
  90. }
  91. let path = URL.createObjectURL(file);
  92. this.files.set(file.name,{file,path,name: file.name,size: file.size,progress: 0,type: 'waiting'});
  93. this.callChange();
  94. this.instantly&&this.upload();
  95. }
  96. },
  97. /**
  98. * @returns {Map} 已选择的文件Map集
  99. */
  100. callChange() {
  101. location.href = 'callback?retype=change&files=' + escape(JSON.stringify([...this.files]));
  102. },
  103. /**
  104. * @returns {object} 正在处理的当前对象
  105. */
  106. changeFilesItem(item,end='') {
  107. this.files.set(item.name,item);
  108. location.href = 'callback?retype=progress&end='+ end +'&item=' + escape(JSON.stringify(item));
  109. },
  110. createUpload(item) {
  111. this.debug&&console.log('准备上传,option=:'+JSON.stringify(this.option));
  112. item.type = 'loading';
  113. delete item.responseText;
  114. return new Promise((resolve,reject)=>{
  115. let {url,name,method='POST',header={},formData={}} = this.option;
  116. let form = new FormData();
  117. for (let keys in formData) {
  118. form.append(keys, formData[keys])
  119. }
  120. form.append(name, item.file);
  121. let xmlRequest = new XMLHttpRequest();
  122. xmlRequest.open(method, url, true);
  123. for (let keys in header) {
  124. xmlRequest.setRequestHeader(keys, header[keys])
  125. }
  126. xmlRequest.upload.addEventListener(
  127. 'progress',
  128. event => {
  129. if (event.lengthComputable) {
  130. let progress = Math.ceil((event.loaded * 100) / event.total)
  131. if (progress <= 100) {
  132. item.progress = progress;
  133. this.changeFilesItem(item);
  134. }
  135. }
  136. },
  137. false
  138. );
  139. xmlRequest.ontimeout = () => {
  140. console.error('请求超时')
  141. item.type = 'fail';
  142. this.changeFilesItem(item,true);
  143. return resolve(false);
  144. }
  145. xmlRequest.onreadystatechange = ev => {
  146. if (xmlRequest.readyState == 4) {
  147. if (xmlRequest.status == 200) {
  148. this.debug && console.log('上传完成:' + xmlRequest.responseText)
  149. item['responseText'] = xmlRequest.responseText;
  150. item.type = 'success';
  151. this.changeFilesItem(item,true);
  152. return resolve(true);
  153. } else if (xmlRequest.status == 0) {
  154. console.error('status = 0 :请检查请求头Content-Type与服务端是否匹配,服务端已正确开启跨域,并且nginx未拦截阻止请求')
  155. }
  156. console.error('--ERROR--:status = ' + xmlRequest.status)
  157. item.type = 'fail';
  158. this.changeFilesItem(item,true);
  159. return resolve(false);
  160. }
  161. }
  162. xmlRequest.send(form)
  163. });
  164. }
  165. }
  166. });
  167. </script>
  168. </body>
  169. </html>