UpLoad.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <template>
  2. <el-upload
  3. name="upload"
  4. class="avatar-img-uploader"
  5. action="/api/meeting/upload/file/"
  6. drag
  7. :headers="Authorization"
  8. :show-file-list="false"
  9. :on-success="handleAvatarSuccess"
  10. :before-upload="beforeAvatarUpload">
  11. <div v-if="imageUrl" class="upload-img-box">
  12. <img :src="imageUrl"/>
  13. <p>{{fileName}}</p>
  14. </div>
  15. <div v-else class="upload-btn-box">
  16. <div>
  17. <div class="upload-btn">+</div>
  18. <div slot="tip" class="el-upload__tip">JPG/JPEG/PNG</div>
  19. </div>
  20. </div>
  21. </el-upload>
  22. </template>
  23. <script>
  24. import { getToken } from "@/utils/auth";
  25. export default {
  26. data(){
  27. return {
  28. imageUrl: "",
  29. fileName: '',
  30. Authorization: {},
  31. }
  32. },
  33. methods: {
  34. handleAvatarSuccess(res, file) {
  35. // console.log('上传成功',res)
  36. // console.log('上传成功',file)
  37. this.$emit('getphotoUrl', res.data.path);
  38. this.fileName = file.name;
  39. this.imageUrl = URL.createObjectURL(file.raw);
  40. },
  41. beforeAvatarUpload(file) {
  42. this.Authorization.Authorization = 'Bearer ' + getToken();
  43. this.loading = true;
  44. const isJPG = file.type === "image/jpeg" || file.type === "image/jpg";
  45. const isPNG = file.type === "image/png";
  46. const isLt2M = file.size / 1024 / 1024 < 2;
  47. if (!isJPG && !isPNG) {
  48. this.$message.error("Upload images can only be in JPG or PNG format!");
  49. }
  50. if (!isLt2M) {
  51. this.$message.error("The size of the uploaded image cannot exceed 2MB!");
  52. }
  53. return (isJPG || isPNG) && isLt2M;
  54. },
  55. }
  56. }
  57. </script>
  58. <style scoped>
  59. .avatar-img-uploader{
  60. width: 100%;
  61. text-align: center;
  62. }
  63. .avatar-img-uploader .el-upload {
  64. border: 1px dashed #000;
  65. border-radius: 6px;
  66. cursor: pointer;
  67. position: relative;
  68. overflow: hidden;
  69. }
  70. .avatar-img-uploader .el-upload:hover {
  71. border-color: #409eff;
  72. }
  73. .avatar-img-uploader-icon {
  74. display: inline-block;
  75. width: 160px;
  76. height: 220px;
  77. overflow: hidden;
  78. border: 1px dashed #aaa;
  79. }
  80. .upload-img-box {
  81. width: 100%;
  82. height: 100%;
  83. padding: 10px;
  84. box-sizing: border-box;
  85. }
  86. .upload-img-box img{
  87. max-width: 100%;
  88. max-height: calc(100% - 40px);
  89. }
  90. .upload-img-box p{
  91. margin: 0;
  92. height: 40px;
  93. line-height: 40px;
  94. font-size: 14px;
  95. color: #666;
  96. line-height: 100%;
  97. width: 100%;
  98. overflow: hidden;
  99. text-overflow: ellipsis;
  100. white-space: nowrap;
  101. }
  102. .upload-btn-box {
  103. display: table;
  104. height: 100%;
  105. width: 100%;
  106. }
  107. .upload-btn-box>div {
  108. display: table-cell;
  109. vertical-align: middle;
  110. }
  111. .upload-btn{
  112. display: inline-block;
  113. width: 30px;
  114. height: 30px;
  115. text-align: center;
  116. line-height: 24px;
  117. font-size: 40px;
  118. border: 3px solid #666;
  119. color: #666;
  120. border-radius: 6px;
  121. }
  122. .el-upload__tip {
  123. line-height: 20px;
  124. }
  125. .avatar-img-uploader /deep/ .el-upload{
  126. width: 100%;
  127. height: 100%;
  128. }
  129. .avatar-img-uploader /deep/ .el-upload-dragger{
  130. width: 100%;
  131. height: 270px;
  132. }
  133. </style>