MultUpload.vue 4.1 KB

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