multi-uploader.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <template>
  2. <div
  3. class="multi-uploader"
  4. :class="fileList && fileList.length >= limit ? 'multi-uploader-limit' : ''"
  5. >
  6. <el-upload
  7. action="/upload_image"
  8. list-type="picture-card"
  9. :limit="limit"
  10. accept=".jpg,.jpeg,.png,.JPG,.PNG,.JPEG"
  11. ref="multiUploader"
  12. with-credentials
  13. :file-list="fileList"
  14. :before-upload="beforeUpload"
  15. :on-preview="handlePictureCardPreview"
  16. :on-remove="handleRemove"
  17. :on-success="handleSuccess"
  18. >
  19. <i class="el-icon-plus"></i>
  20. </el-upload>
  21. <el-dialog :visible.sync="dialogVisible">
  22. <img width="100%" :src="dialogImageUrl" alt />
  23. </el-dialog>
  24. <div
  25. slot="tip"
  26. v-if="showTips"
  27. class="el-upload__tip"
  28. :class="fileList.length ? 'el-upload__tip-active' : ''"
  29. >
  30. {{ tips }}
  31. </div>
  32. </div>
  33. </template>
  34. <script>
  35. export default {
  36. name: "multi-uploader",
  37. props: {
  38. value: {
  39. type: Array
  40. },
  41. showTips: {
  42. type: Boolean,
  43. default: true
  44. },
  45. limit: {
  46. type: Number,
  47. default: 9
  48. },
  49. tips: {
  50. type: String,
  51. default: "最多添加9张作品图片"
  52. },
  53. fileSize: {
  54. type: Number,
  55. default: 5
  56. }
  57. },
  58. model: {
  59. prop: "value",
  60. event: "change"
  61. },
  62. components: {},
  63. data() {
  64. return {
  65. uploading: false,
  66. fileList: [],
  67. dialogImageUrl: "",
  68. dialogVisible: false
  69. };
  70. },
  71. watch: {
  72. value: {
  73. //深度监听,可监听到对象、数组的变化
  74. handler(val, oldVal) {
  75. console.log("watch1:", val);
  76. this.fileList = val;
  77. },
  78. deep: true //true 深度监听
  79. },
  80. fileList: {
  81. //深度监听,可监听到对象、数组的变化
  82. handler(val, oldVal) {
  83. console.log("watch2:", val);
  84. this.$emit("change", val);
  85. },
  86. deep: true //true 深度监听
  87. }
  88. },
  89. computed: {},
  90. mounted() {
  91. this.fileList = this.value;
  92. },
  93. methods: {
  94. beforeUpload(file) {
  95. console.log("beforeUpload", file);
  96. console.log("before", this.$refs.multiUploader);
  97. let fileType = file.type ? file.type : file.raw ? file.raw.type : "";
  98. if (
  99. fileType !== "image/png" &&
  100. fileType !== "image/jpg" &&
  101. fileType !== "image/jpeg" &&
  102. fileType !== "image/gif"
  103. ) {
  104. this.$message.error("请上传png,jp(e)g,gif格式的图片");
  105. return false;
  106. }
  107. if (file.size / 1024 / 1024 > this.fileSize) {
  108. this.$message.error("图片大小不可超过5M");
  109. return false;
  110. }
  111. return true;
  112. },
  113. handleRemove(file, fileList) {
  114. console.log(file, fileList);
  115. this.fileList = fileList;
  116. },
  117. handlePictureCardPreview(file) {
  118. this.dialogImageUrl = file.url;
  119. this.dialogVisible = true;
  120. },
  121. handleSuccess(response, file, fileList) {
  122. console.log(response, file, fileList, this.fileList);
  123. if (!response.error && response.filename) {
  124. console.log("上传成功");
  125. const list = [];
  126. fileList.forEach(fileItem => {
  127. list.push({
  128. name: fileItem.name,
  129. url: fileItem.response ? fileItem.response.filename : fileItem.url,
  130. uid: fileItem.uid,
  131. status: "success"
  132. });
  133. });
  134. this.fileList = list;
  135. } else {
  136. this.$refs.multiUploader.abort(file);
  137. const list = [];
  138. fileList.forEach(tmpFile => {
  139. if (file.uid !== tmpFile.uid) {
  140. list.push({
  141. name: tmpFile.name,
  142. url: tmpFile.response ? tmpFile.response.filename : tmpFile.url,
  143. uid: tmpFile.uid,
  144. status: "success"
  145. });
  146. }
  147. });
  148. this.fileList = list;
  149. console.log("上传失败 abort");
  150. }
  151. }
  152. }
  153. };
  154. </script>
  155. <style lang="scss">
  156. .multi-uploader {
  157. position: relative;
  158. background: #fff;
  159. }
  160. .multi-uploader-limit {
  161. .el-upload--picture-card {
  162. display: none;
  163. }
  164. }
  165. .el-upload__tip {
  166. line-height: 20px;
  167. // margin-top: 10px;
  168. font-size: 14px;
  169. color: #999999;
  170. }
  171. .el-upload__tip-active {
  172. margin-top: -1px;
  173. }
  174. </style>