c2bce9cf0f947d559f55289a6488262a45ea7215.svn-base 4.2 KB

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