multi-uploader.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <template>
  2. <div class="multi-uploader">
  3. <el-upload
  4. action="/upload_image"
  5. list-type="picture-card"
  6. :limit="9"
  7. accept=".jpg,.jpeg,.png,.git,.JPG,.PNG,.JPEG,.GIF"
  8. ref="multiUploader"
  9. with-credentials
  10. :file-list="fileList"
  11. :before-upload="beforeUpload"
  12. :on-preview="handlePictureCardPreview"
  13. :on-remove="handleRemove"
  14. :on-success="handleSuccess"
  15. >
  16. <i class="el-icon-plus"></i>
  17. </el-upload>
  18. <el-dialog :visible.sync="dialogVisible">
  19. <img width="100%" :src="dialogImageUrl" alt/>
  20. </el-dialog>
  21. <div slot="tip" class="el-upload__tip">最多添加9张作品图片</div>
  22. </div>
  23. </template>
  24. <script>
  25. export default {
  26. name: "multi-uploader",
  27. props: {
  28. value: {
  29. type: Array
  30. }
  31. },
  32. model: {
  33. prop: "value",
  34. event: "change"
  35. },
  36. components: {},
  37. data() {
  38. return {
  39. uploading: false,
  40. fileList: this.value,
  41. dialogImageUrl: "",
  42. dialogVisible: false
  43. };
  44. },
  45. watch: {
  46. value: {//深度监听,可监听到对象、数组的变化
  47. handler(val, oldVal) {
  48. console.log('watch1:', val)
  49. this.fileList = val
  50. },
  51. deep: true //true 深度监听
  52. },
  53. fileList: {//深度监听,可监听到对象、数组的变化
  54. handler(val, oldVal) {
  55. console.log('watch2:', val)
  56. this.$emit("change", val);
  57. },
  58. deep: true //true 深度监听
  59. }
  60. },
  61. computed: {},
  62. mounted() {
  63. this.fileList = this.value;
  64. },
  65. methods: {
  66. beforeUpload(file) {
  67. console.log("beforeUpload", file);
  68. console.log("before", this.$refs.multiUploader);
  69. let fileType = file.type ? file.type : (file.raw ? file.raw.type : "");
  70. if (fileType !== "image/png" && fileType !== "image/jpg" && fileType !== "image/jpeg" && fileType !== "image/gif") {
  71. this.$message.error("请上传png,jpg格式的图片");
  72. return false;
  73. }
  74. if (file.size / 1024 / 1024 > 2) {
  75. this.$message.error("图片大小超过2M");
  76. return false;
  77. }
  78. return true;
  79. },
  80. handleRemove(file, fileList) {
  81. console.log(file, fileList);
  82. },
  83. handlePictureCardPreview(file) {
  84. this.dialogImageUrl = file.url;
  85. this.dialogVisible = true;
  86. },
  87. handleSuccess(response, file, fileList) {
  88. console.log(response, file, fileList);
  89. if (!response.error && response.filename) {
  90. console.log("上传成功");
  91. const list = [];
  92. fileList.map(fileItem => {
  93. list.push({
  94. name: fileItem.name,
  95. url: fileItem.response ? fileItem.response.filename : fileItem.url
  96. });
  97. });
  98. this.fileList = list;
  99. } else {
  100. this.$refs.multiUploader.abort(file);
  101. const list = [];
  102. fileList.map(tmpFile => {
  103. if (file.uid !== tmpFile.uid) {
  104. list.push({
  105. name: file.name,
  106. url: file.response ? file.response.filename : file.url
  107. });
  108. }
  109. });
  110. this.fileList = list;
  111. console.log("上传失败 abort");
  112. }
  113. },
  114. }
  115. };
  116. </script>
  117. <style lang="scss">
  118. .multi-uploader {
  119. position: relative;
  120. background: #fff;
  121. }
  122. </style>