uploader.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <template>
  2. <div class="uploader">
  3. <el-upload
  4. class="avatar-uploader"
  5. action="#"
  6. :show-file-list="false"
  7. :multiple="false"
  8. accept="image/png, image/jpeg"
  9. :before-upload="handleFileChange"
  10. >
  11. <i v-if="imageUrl" class="el-icon-delete avatar-uploader-icon" @click.stop="handleDeleteFile"></i>
  12. <img v-if="imageUrl" :src="imageUrl" class="avatar" />
  13. <i v-else class="el-icon-plus avatar-uploader-icon"></i>
  14. <span v-if="title" class="title">{{title}}</span>
  15. </el-upload>
  16. </div>
  17. </template>
  18. <script>
  19. export default {
  20. props: ["imageUrl", "title", "limitFileSize"],
  21. components: {},
  22. data() {
  23. return {
  24. uploading: false
  25. };
  26. },
  27. computed: {},
  28. mounted() {},
  29. methods: {
  30. handleDeleteFile() {
  31. this.$emit("change", "");
  32. },
  33. handleFileChange(file) {
  34. if (this.limitFileSize && this.limitFileSize > 0) {
  35. if (file.size / (1024 * 1024) > this.limitFileSize) {
  36. this.$message.error(`图片大小不得超过${this.limitFileSize}M,请重新选择`);
  37. return false;
  38. }
  39. } else if (file.size / 1024 > 2048) {
  40. this.$message.error("图片大小不得超过2M,请重新选择");
  41. return false;
  42. }
  43. const formData = new FormData();
  44. formData.append("file", file);
  45. formData.append("original_filename", file.name);
  46. this.uploading = true;
  47. this.$axios
  48. .$post(`/upload_image`, formData, {
  49. headers: { "Content-Type": "multipart/form-data" }
  50. })
  51. .then(res => {
  52. this.$emit("change", res.filename);
  53. })
  54. .finally(() => {
  55. this.uploading = false;
  56. });
  57. }
  58. }
  59. };
  60. </script>
  61. <style lang="scss">
  62. .uploader {
  63. position: relative;
  64. background: #fff;
  65. .el-icon-delete {
  66. display: none;
  67. }
  68. .avatar-uploader .el-upload {
  69. width: 184px;
  70. height: 162px;
  71. border: 1px dashed #d9d9d9;
  72. border-radius: 6px;
  73. cursor: pointer;
  74. position: relative;
  75. overflow: hidden;
  76. img {
  77. width: 100%;
  78. height: auto;
  79. object-fit: contain;
  80. object-position: top left;
  81. }
  82. }
  83. .avatar-uploader .el-upload:hover {
  84. border-color: #409eff;
  85. .el-icon-delete {
  86. display: block;
  87. }
  88. }
  89. .avatar-uploader-icon {
  90. position: absolute;
  91. top: 0;
  92. left: 0;
  93. font-size: 44px;
  94. color: #dce1e8;
  95. width: 184px;
  96. height: 162px;
  97. line-height: 160px;
  98. text-align: center;
  99. /* background: rgba(1, 1, 1, 0.1); */
  100. :hover {
  101. color: #409eff;
  102. }
  103. }
  104. .avatar {
  105. width: 184px;
  106. height: 162px;
  107. display: block;
  108. }
  109. .title {
  110. position: absolute;
  111. left: 50%;
  112. bottom: 8px;
  113. transform: translateX(-50%);
  114. font-size: 13px;
  115. font-family: PingFangSC-Medium;
  116. font-weight: 500;
  117. color: rgba(25, 34, 46, 1);
  118. line-height: 18px;
  119. }
  120. }
  121. </style>