a75de85a3c821fc6d88828f57fb79890b373e62a.svn-base 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <!--
  2. <upload ref="uploadFile" url="/biz/hzz/basic/file/upload?bizId=" @uploadOver="uploadOver"></upload>
  3. if (this.$refs.uploadFile.submitUpload(id)) {
  4. // true上传列表为空,直接执行结束操作
  5. this.insertOver()
  6. }
  7. -->
  8. <template>
  9. <div style="width: 360px;margin: 20px auto;">
  10. <el-upload
  11. ref="upload"
  12. :action="url"
  13. :headers="header"
  14. :accept="accept"
  15. :auto-upload="true"
  16. multiple
  17. :on-change="changeFileList"
  18. :on-success="uploadSuccess"
  19. :on-error="upError"
  20. >
  21. <el-button size="small" type="primary" style="margin: 0">上传</el-button>
  22. </el-upload>
  23. </div>
  24. </template>
  25. <script>
  26. export default {
  27. props: ["url","accept"], // 上传基础路径
  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. },
  39. };
  40. },
  41. computed:{
  42. userid(){
  43. return localStorage.getItem('userid') ? localStorage.getItem('userid') : ''
  44. }
  45. },
  46. methods: {
  47. // 初始化,
  48. init() {
  49. this.$refs.upload.clearFiles();
  50. this.length = 0;
  51. this.sum = 0;
  52. this.allurl = "";
  53. this.resList = [];
  54. },
  55. // 开始上传,文件列表为空直接返回turn表示结束,不为空触发上传并返回false,由uploadOver判断结束时间
  56. // submitUpload(id) {
  57. // this.allurl = this.url + id;
  58. // if (this.length) {
  59. // this.$nextTick(() => {
  60. // this.$refs.upload.submit();
  61. // });
  62. // return false;
  63. // } else {
  64. // return true;
  65. // }
  66. // },
  67. // 上传出错,向上触发结束
  68. upError(err, file, fileList) {
  69. this.$message.error("上传出错,请修改文件!");
  70. this.$emit("uploadOver");
  71. this.init();
  72. },
  73. // 全部上传成功,向上触发结束
  74. uploadSuccess(response, file, fileList) {
  75. if(!response.success){
  76. this.$message.error("上传出错,请修改文件!");
  77. this.$emit("uploadOver");
  78. this.init();
  79. return
  80. }
  81. this.resList.push(response.data);
  82. console.log(this.resList);
  83. this.sum = this.sum + 1;
  84. if (this.sum == this.length) {
  85. this.$emit("uploadOver", this.resList);
  86. this.init();
  87. }
  88. },
  89. // 及时更新列表长度
  90. changeFileList(file, fileList) {
  91. this.length = fileList.length;
  92. }
  93. }
  94. };
  95. </script>