You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

form.js 5.5 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /** Copyright 2020 Zhejiang Lab. All Rights Reserved.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. * =============================================================
  15. */
  16. import axios from 'axios';
  17. import { Message } from 'element-ui';
  18. import './style.scss';
  19. // eslint-disable-next-line import/no-extraneous-dependencies
  20. const path = require('path');
  21. let msgInstance;
  22. const defaultAccept = d => `${d} MB`;
  23. export default {
  24. name: 'UploadForm',
  25. props: {
  26. action: String,
  27. accept: {
  28. type: String,
  29. default: '.jpg,.png,.bmp,.jpeg',
  30. },
  31. acceptSize: {
  32. type: Number, // 如果传入 0 代表不限制
  33. default: 5, // MB
  34. },
  35. acceptSizeFormat: { // 格式化文本
  36. type: Function,
  37. default: defaultAccept,
  38. },
  39. limit: {
  40. type: Number,
  41. default: 5000,
  42. },
  43. showFileCount: {
  44. type: Boolean,
  45. default: true,
  46. },
  47. },
  48. data() {
  49. return {
  50. uploading: false,
  51. lenOfFileList: 0,
  52. source: axios.CancelToken.source(),
  53. };
  54. },
  55. computed: {
  56. acceptFormatStr() {
  57. const formats = this.accept.split(',');
  58. return formats.join('/');
  59. },
  60. },
  61. methods: {
  62. onMessageClose() {
  63. // 清理 message 实例
  64. msgInstance = null;
  65. },
  66. reset() {
  67. this.$refs.uploader.clearFiles();
  68. this.lenOfFileList = 0;
  69. },
  70. fileChange(file, fileList) {
  71. // 根据后缀名进行格式匹配
  72. const acceptTypes = this.accept.split(',');
  73. const extname = path.extname(file.raw.name);
  74. const mimeType = acceptTypes.includes(extname.toLowerCase());
  75. if (!mimeType) {
  76. fileList.splice(fileList.indexOf(file), 1);
  77. if (!msgInstance) {
  78. Message.info({
  79. message: `文件格式不支持`,
  80. onClose: this.onMessageClose,
  81. });
  82. }
  83. return;
  84. }
  85. // accept 支持传入 0 代表不限制大小
  86. const isOverSize = this.acceptSize !== 0 && (file.size / (1024 * 1024)) > this.acceptSize;
  87. if (isOverSize) {
  88. fileList.splice(fileList.indexOf(file), 1);
  89. if (!msgInstance) {
  90. msgInstance = Message.info({
  91. message: `不能添加大于${this.acceptSize}MB的文件`,
  92. onClose: this.onMessageClose,
  93. });
  94. }
  95. return;
  96. }
  97. for (const item of fileList.slice(0, fileList.length - 1)) {
  98. if (item.name === file.name) {
  99. fileList.splice(fileList.indexOf(file), 1);
  100. if (!msgInstance) {
  101. msgInstance = Message.info({
  102. message: `不能添加文件名相同的文件`,
  103. onClose: this.onMessageClose,
  104. });
  105. }
  106. return false;
  107. }
  108. }
  109. this.lenOfFileList = fileList.length;
  110. // 触发文件变动事件
  111. this.$emit('fileChange', file, fileList);
  112. },
  113. onProgress(res) {
  114. const {loaded} = res;
  115. const {total} = res;
  116. const uploadPercent = Math.floor((loaded / total) * 100) > 1 ? Math.floor((loaded / total) * 100) : 1;
  117. this.$emit('onUploadPercent', uploadPercent);
  118. },
  119. onRemove(file, fileList) {
  120. this.lenOfFileList = fileList.length;
  121. this.$attrs['on-remove'] && this.$attrs['on-remove'](file, fileList);
  122. },
  123. cancelUpload() {
  124. if (this.source) {
  125. this.source.cancel('取消上传');
  126. }
  127. },
  128. onExceed(files, fileList) {
  129. if (files.length > this.limit || fileList.length > this.limit) {
  130. Message.info(`单次上传文件数量不能超过${this.limit}`);
  131. }
  132. },
  133. },
  134. render(h) {
  135. // vue jsx 属性传递需要把 on- 放到 props 内
  136. // 详细参考:https://zhuanlan.zhihu.com/p/37920151
  137. const uploadProps = {
  138. props: {
  139. onChange: this.fileChange,
  140. onRemove: this.onRemove,
  141. onExceed: this.onExceed,
  142. ...this.$attrs,
  143. },
  144. ref: 'uploader',
  145. };
  146. return (
  147. <div id='upload-form-style' class='upload-form'>
  148. <el-upload
  149. action={this.action}
  150. accept={this.accept}
  151. class='upload-field'
  152. limit={this.limit}
  153. multiple
  154. list-type={this.lenOfFileList>100? 'text' : 'picture'}
  155. auto-upload={false}
  156. disabled={this.uploading}
  157. {...uploadProps}
  158. >
  159. <el-button disabled={this.uploading} size='mini' icon='el-icon-upload'>上传文件</el-button>
  160. <div slot='tip' class='flex f1 flex-between' style='margin-left: 20px;'>
  161. <div class='upload-tip'>
  162. <span>文件格式: { this.acceptFormatStr }</span>
  163. {
  164. this.acceptSize > 0 && (
  165. <span>, 文件不大于 { this.acceptSizeFormat(this.acceptSize) }</span>
  166. )
  167. }
  168. </div>
  169. {
  170. this.showFileCount && (
  171. <span class='upload-chosen-tip'>已选择{ this.lenOfFileList }张</span>
  172. )
  173. }
  174. </div>
  175. </el-upload>
  176. </div>
  177. );
  178. },
  179. };

一站式算法开发平台、高性能分布式深度学习框架、先进算法模型库、视觉模型炼知平台、数据可视化分析平台等一系列平台及工具,在模型高效分布式训练、数据处理和可视分析、模型炼知和轻量化等技术上形成独特优势,目前已在产学研等各领域近千家单位及个人提供AI应用赋能

Contributors (1)