6d3150a657a03ffe9e8ee9a1fa1bb4edb615f1cd.svn-base 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <template>
  2. <div class="inline-auto-upload">
  3. <el-upload
  4. ref="upload"
  5. :action="url"
  6. :headers="header"
  7. :accept="accept"
  8. :auto-upload="true"
  9. :limit="limit"
  10. :on-change="changeFileList"
  11. :on-remove="handleRemove"
  12. :on-success="uploadSuccess"
  13. :on-error="upError"
  14. :file-list="fileList"
  15. :before-upload="beforeAvatarUpload"
  16. :on-exceed="handleExceed"
  17. >
  18. <el-button size="mini" type="primary" class="el-icon-upload2">{{desc}}</el-button>
  19. <div slot="tip" class="el-upload__tip">{{autoComment}}</div>
  20. </el-upload>
  21. </div>
  22. </template>
  23. <script>
  24. export default {
  25. components: {},
  26. props: ["url", "accept", "gwComFiles", "fileDesc", "comment","uuid","limit"], // 上传基础路径
  27. data() {
  28. return {
  29. fileList: [],
  30. resList: [],
  31. length: 0, // 上传列表长度
  32. sum: 0, // 上传成功次数
  33. allurl: "", // 上传完整路径
  34. header: {
  35. 'persId': localStorage.getItem('userid'),
  36. 'accessToken' : localStorage.getItem('accessToken'),
  37. 'orgId': localStorage.getItem('currentOrgId')
  38. },
  39. desc: "直接导入",
  40. autoComment: "注:.xls格式,不超过50MB",
  41. autoUrl: "",
  42. filePath: "",
  43. fileName: "",
  44. dialogVisible: false
  45. };
  46. },
  47. created() {},
  48. mounted() {
  49. if (this.gwComFiles) {
  50. this.fileList = this.gwComFiles;
  51. }
  52. if (this.fileDesc) {
  53. this.desc = this.fileDesc;
  54. }
  55. if (this.comment) {
  56. this.autoComment = this.comment;
  57. }
  58. // if (this.url) {
  59. // this.autoUrl = this.url;
  60. // }
  61. },
  62. computed: {},
  63. methods: {
  64. // 初始化,
  65. init() {
  66. this.length = 0;
  67. this.sum = 0;
  68. this.resList = [];
  69. this.$refs.upload.clearFiles();
  70. },
  71. // 开始上传,文件列表为空直接返回turn表示结束,不为空触发上传并返回false,由uploadOver判断结束时间
  72. submitUpload() {
  73. if (this.length) {
  74. this.$nextTick(() => {
  75. this.$refs.upload.submit();
  76. });
  77. return false;
  78. } else {
  79. return true;
  80. }
  81. },
  82. // 上传出错,向上触发结束
  83. upError(err, file, fileList) {
  84. this.$message.error("上传出错,请修改文件!");
  85. this.$emit("uploadOver");
  86. this.init();
  87. },
  88. // 全部上传成功,向上触发结束
  89. uploadSuccess(response, file, fileList) {
  90. if (!response.success) {
  91. this.$message.error("上传出错,请修改文件!");
  92. this.$emit("uploadOver");
  93. this.init();
  94. return;
  95. }
  96. this.$message.success("上传文件成功");
  97. this.resList.push(response.data);
  98. console.log(this.resList);
  99. this.sum = this.sum + 1;
  100. if (this.sum == this.length) {
  101. this.$emit("uploadOver");
  102. this.init();
  103. }
  104. },
  105. // 及时更新列表长度
  106. changeFileList(file, fileList) {
  107. this.length = fileList.length;
  108. this.$emit("changeFileList", fileList);
  109. },
  110. handleRemove(file, fileList) {
  111. this.length = fileList.length;
  112. this.$emit("handleRemove", fileList);
  113. },
  114. beforeAvatarUpload(file) {
  115. let extArr = file.name.split(".");
  116. let acceptExt = this.accept;
  117. let fileSize = file.size / 1024 <= 51200;
  118. if (extArr && acceptExt.indexOf(extArr[extArr.length - 1]) < 0) {
  119. this.$message.error("上传文件格式只能为.xls格式");
  120. return false;
  121. }
  122. if(!fileSize){
  123. this.$message.error("上传文件大小不超过50MB");
  124. return false;
  125. }
  126. return true;
  127. },
  128. handleExceed(files, fileList) {
  129. this.$message.warning(
  130. `当前限制选择 1 个文件,本次选择了 ${
  131. files.length
  132. } 个文件,共选择了 ${files.length + fileList.length} 个文件`
  133. );
  134. },
  135. handleClose() {
  136. this.dialogVisible = false;
  137. }
  138. }
  139. };
  140. </script>
  141. <style scoped>
  142. .el-dialog__wrapper /deep/.el-dialog__title{
  143. font-size: 20px !important;
  144. font-weight: bold;
  145. color:red;
  146. }
  147. .inline-auto-upload{
  148. display: inline-flex!important;
  149. }
  150. .inline-auto-upload div:first-child{
  151. display:flex;
  152. }
  153. .el-upload__tip{
  154. font-size:10px;
  155. color:red;
  156. }
  157. </style>