2d051142eef827d8a7d04e3179b6757cab9468bc.svn-base 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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: 420px;margin: 20px auto;">
  10. <el-upload
  11. ref="upload"
  12. class="upload-demo"
  13. :action="allurl"
  14. :accept="accept"
  15. :headers="header"
  16. :auto-upload="false"
  17. :before-upload="beforeUpload"
  18. :on-preview="handlePictureCardPreview"
  19. :on-remove="handleRemove"
  20. :on-change="changeFileList"
  21. :on-success="uploadSuccess"
  22. :on-error="upError"
  23. >
  24. <el-button size="small" type="primary">点击上传</el-button>
  25. </el-upload>
  26. <el-dialog :visible.sync="dialogVisible" append-to-body>
  27. <img width="100%" :src="dialogImageUrl" alt="">
  28. </el-dialog>
  29. <el-form ref="form" label-width="80px" style="margin-top: 20px;">
  30. <el-form-item label="描述信息">
  31. <el-input v-model="description" type="textarea" rows="3"></el-input>
  32. </el-form-item>
  33. </el-form>
  34. </div>
  35. </template>
  36. <script>
  37. export default {
  38. props: ["url","accept"], // 上传基础路径
  39. data() {
  40. return {
  41. fileList: [],
  42. resList: [],
  43. length: 0, // 上传列表长度
  44. sum: 0, // 上传成功次数
  45. allurl: "", // 上传完整路径
  46. header: {
  47. 'persId': localStorage.getItem('userid'),
  48. 'accessToken' : localStorage.getItem('accessToken')
  49. },
  50. dialogImageUrl: '',
  51. dialogVisible: false,
  52. description: ''
  53. };
  54. },
  55. computed:{
  56. userid(){
  57. return localStorage.getItem('userid') ? localStorage.getItem('userid') : ''
  58. }
  59. },
  60. methods: {
  61. // 初始化,
  62. init() {
  63. this.$refs.upload.clearFiles();
  64. this.length = 0;
  65. this.sum = 0;
  66. this.allurl = "";
  67. this.resList = [];
  68. },
  69. // 开始上传,文件列表为空直接返回turn表示结束,不为空触发上传并返回false,由uploadOver判断结束时间
  70. submitUpload(id,type) {
  71. this.allurl = `${this.url}${id}&bizType=${type}&keyWord=${this.description}`;
  72. if (this.length) {
  73. this.$nextTick(() => {
  74. this.$refs.upload.submit();
  75. this.description = ''
  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. this.resList.push(response.data);
  91. console.log(this.resList);
  92. this.sum = this.sum + 1;
  93. if (this.sum == this.length) {
  94. this.$emit("uploadOver", this.resList);
  95. this.init();
  96. }
  97. },
  98. // 及时更新列表长度
  99. changeFileList(file, fileList) {
  100. this.length = fileList.length;
  101. },
  102. handleRemove(file, fileList) {
  103. console.log(file, fileList);
  104. },
  105. handlePictureCardPreview(file) {
  106. this.dialogImageUrl = file.url;
  107. this.dialogVisible = true;
  108. },
  109. beforeUpload(file, fileList) {
  110. if (file.size / (1024 * 1024) > 100) { // 限制文件大小
  111. this.$message.warning(`当前限制文件大小不能大于100M`)
  112. return false
  113. }
  114. }
  115. }
  116. };
  117. </script>