MultUpload.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <template>
  2. <div>
  3. <el-upload
  4. v-loading.fullscreen.lock="mark"
  5. ref="upload"
  6. action=""
  7. :headers="myHeaders"
  8. list-type="picture-card"
  9. :multiple="true"
  10. :auto-upload="false"
  11. :limit="limit"
  12. :on-change="handleChange"
  13. :disabled="limit <= value.length"
  14. >
  15. <i slot="default" class="el-icon-plus" />
  16. <div slot="file" slot-scope="{ file }">
  17. <img class="el-upload-list__item-thumbnail" :src="file.url" alt="" />
  18. <span class="el-upload-list__item-actions">
  19. <span
  20. class="el-upload-list__item-preview"
  21. @click="handlePictureCardPreview(file)"
  22. >
  23. <i class="el-icon-zoom-in" />
  24. </span>
  25. </span>
  26. </div>
  27. </el-upload>
  28. <el-button
  29. class="mt-40"
  30. style="margin-left: 10px;"
  31. size="small"
  32. type="success"
  33. @click="submitUpload"
  34. >上传到服务器</el-button
  35. >
  36. <el-dialog :visible.sync="dialogVisible" :modal="false">
  37. <img width="100%" :src="dialogImageUrl" alt="" />
  38. </el-dialog>
  39. </div>
  40. </template>
  41. <script>
  42. import { UPLOAD_URL } from '@/const/urlKey';
  43. import { getToken } from '@/utils/auth';
  44. export default {
  45. name: 'Upload',
  46. props: {
  47. value: {
  48. type: Array,
  49. default: () => []
  50. },
  51. limit: {
  52. type: Number,
  53. default: 1
  54. },
  55. params: {
  56. type: String,
  57. default: ''
  58. },
  59. paramsData: {
  60. type: Object,
  61. default: () => ({})
  62. },
  63. api: {
  64. type: Function,
  65. default: () => {}
  66. }
  67. },
  68. data() {
  69. return {
  70. mark: false,
  71. url: UPLOAD_URL,
  72. dialogImageUrl: '',
  73. dialogVisible: false,
  74. disabled: false,
  75. myHeaders: {
  76. authorization: 'Bearer swagger-ui',
  77. token: getToken()
  78. },
  79. imgList: [],
  80. tmp: []
  81. };
  82. },
  83. computed: {
  84. newUrl() {
  85. const params = this.params;
  86. const url = this.uploadUrl;
  87. let baseUrl = UPLOAD_URL;
  88. if (url !== '') {
  89. baseUrl = url;
  90. }
  91. if (params === '') {
  92. return baseUrl;
  93. } else {
  94. return baseUrl + `?${params}`;
  95. }
  96. }
  97. },
  98. // watch: {
  99. // value: {
  100. // handler(val) {
  101. // // if (this.imgList.length < val.length) {
  102. // this.imgList = val;
  103. // console.log(this.imgList);
  104. // // }
  105. // },
  106. // immediate: true
  107. // }
  108. // },
  109. methods: {
  110. handleChange(fileList) {
  111. this.imgList.push(fileList);
  112. },
  113. submitUpload() {
  114. this.mark = true;
  115. let form = new FormData();
  116. for (let key in this.paramsData) {
  117. form.append(key, this.paramsData[key]);
  118. }
  119. console.log(this.imgList);
  120. this.imgList.forEach(file => {
  121. form.append('file', file.raw);
  122. });
  123. // this.mark = false;
  124. this.api(form)
  125. .then(({ success }) => {
  126. if (success) {
  127. this.mark = false;
  128. this.$emit('finish');
  129. } else {
  130. this.$error('上传失败,请稍后重试...');
  131. this.mark = false;
  132. }
  133. })
  134. .catch(err => {
  135. console.error(err);
  136. this.$error('上传失败,请稍后重试...');
  137. this.mark = false;
  138. });
  139. },
  140. handlePictureCardPreview(file) {
  141. this.dialogImageUrl = file.url;
  142. this.dialogVisible = true;
  143. }
  144. }
  145. };
  146. </script>
  147. <style scoped>
  148. .page-select {
  149. display: flex;
  150. justify-content: flex-end;
  151. align-items: center;
  152. padding: 20px;
  153. }
  154. </style>