uploader.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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"],
  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 (file.size / 1024 > 2048) {
  35. this.$message.error("图片大小不得超过2M,请重新选择");
  36. return false;
  37. }
  38. const formData = new FormData();
  39. formData.append("file", file);
  40. formData.append("original_filename", file.name);
  41. this.uploading = true;
  42. this.$axios
  43. .$post(`/upload_image`, formData, {
  44. headers: { "Content-Type": "multipart/form-data" }
  45. })
  46. .then(res => {
  47. this.$emit("change", res.filename);
  48. })
  49. .finally(() => {
  50. this.uploading = false;
  51. });
  52. }
  53. }
  54. };
  55. </script>
  56. <style lang="scss">
  57. .uploader {
  58. position: relative;
  59. background: #fff;
  60. .el-icon-delete {
  61. display: none;
  62. }
  63. .avatar-uploader .el-upload {
  64. width: 184px;
  65. height: 162px;
  66. border: 1px dashed #d9d9d9;
  67. border-radius: 6px;
  68. cursor: pointer;
  69. position: relative;
  70. overflow: hidden;
  71. img {
  72. width: 100%;
  73. height: auto;
  74. object-fit: contain;
  75. object-position: top left;
  76. }
  77. }
  78. .avatar-uploader .el-upload:hover {
  79. border-color: #409eff;
  80. .el-icon-delete {
  81. display: block;
  82. }
  83. }
  84. .avatar-uploader-icon {
  85. position: absolute;
  86. top: 0;
  87. left: 0;
  88. font-size: 44px;
  89. color: #dce1e8;
  90. width: 184px;
  91. height: 162px;
  92. line-height: 160px;
  93. text-align: center;
  94. /* background: rgba(1, 1, 1, 0.1); */
  95. :hover {
  96. color: #409eff;
  97. }
  98. }
  99. .avatar {
  100. width: 184px;
  101. height: 162px;
  102. display: block;
  103. }
  104. .title {
  105. position: absolute;
  106. left: 50%;
  107. bottom: 8px;
  108. transform: translateX(-50%);
  109. font-size: 13px;
  110. font-family: PingFangSC-Medium;
  111. font-weight: 500;
  112. color: rgba(25, 34, 46, 1);
  113. line-height: 18px;
  114. }
  115. }
  116. </style>