7c0b4666e408be1e288a6ca79fe8b07403f93b20.svn-base 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. drag
  13. :action="allurl"
  14. :headers="header"
  15. :auto-upload="false"
  16. multiple
  17. :on-change="changeFileList"
  18. :on-success="uploadSuccess"
  19. :on-error="upError"
  20. >
  21. <i class="el-icon-upload"></i>
  22. <div class="el-upload__text">
  23. 将文件拖到此处,或
  24. <em>点击添加</em>
  25. </div>
  26. </el-upload>
  27. </div>
  28. </template>
  29. <script>
  30. export default {
  31. props: ["url",'type'], // 上传基础路径
  32. data() {
  33. return {
  34. fileList: [],
  35. resList: [],
  36. length: 0, // 上传列表长度
  37. sum: 0, // 上传成功次数
  38. allurl: "", // 上传完整路径
  39. header: {
  40. 'persId': localStorage.getItem('userid'),
  41. 'accessToken' : localStorage.getItem('accessToken')
  42. },
  43. };
  44. },
  45. computed:{
  46. userid(){
  47. return localStorage.getItem('userid') ? localStorage.getItem('userid') : ''
  48. }
  49. },
  50. methods: {
  51. // 初始化,
  52. init() {
  53. this.$refs.upload.clearFiles();
  54. this.length = 0;
  55. this.sum = 0;
  56. this.allurl = "";
  57. this.resList = [];
  58. },
  59. // 开始上传,文件列表为空直接返回turn表示结束,不为空触发上传并返回false,由uploadOver判断结束时间
  60. submitUpload(id) {
  61. this.allurl = `${this.url}${id}&bizType=${this.type}`;
  62. if (this.length) {
  63. this.$nextTick(() => {
  64. this.$refs.upload.submit();
  65. });
  66. return false;
  67. } else {
  68. return true;
  69. }
  70. },
  71. // 上传出错,向上触发结束
  72. upError(err, file, fileList) {
  73. this.$message.error("上传出错,请修改文件!");
  74. this.$emit("uploadOver");
  75. this.init();
  76. },
  77. // 全部上传成功,向上触发结束
  78. uploadSuccess(response, file, fileList) {
  79. this.resList.push(response.data);
  80. console.log(this.resList);
  81. this.sum = this.sum + 1;
  82. if (this.sum == this.length) {
  83. this.$emit("uploadOver", this.resList);
  84. this.init();
  85. }
  86. },
  87. // 及时更新列表长度
  88. changeFileList(file, fileList) {
  89. this.length = fileList.length;
  90. }
  91. }
  92. };
  93. </script>