uploader.vue 2.6 KB

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