upload.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <template>
  2. <section style="display:inline-block;">
  3. <el-upload
  4. class="avatar-uploader"
  5. :action="actionUrl"
  6. :data="data"
  7. name="file"
  8. :show-file-list="false"
  9. :before-upload="addUpload"
  10. :on-success="handleAvatarSuccess"
  11. >
  12. <img v-if="imgUrl" :src="imgUrl" class="avatar" />
  13. <i v-else class="el-icon-plus avatar-uploader-icon"></i>
  14. </el-upload>
  15. </section>
  16. </template>
  17. <script>
  18. export default {
  19. props: {
  20. imgUrl: {
  21. type: String,
  22. default: ""
  23. },
  24. actionUrl: {
  25. type: String,
  26. defalut: ""
  27. },
  28. data: {
  29. type: Object,
  30. defalut: {}
  31. },
  32. limtSize: {
  33. type: Boolean,
  34. defalut: false
  35. },
  36. imgWidth: {
  37. type: Number,
  38. defalutL: 100
  39. },
  40. imgHeight: {
  41. type: Number,
  42. defalutL: 100
  43. }
  44. },
  45. data() {
  46. return {};
  47. },
  48. methods: {
  49. addUpload(file) {
  50. let that = this;
  51. // const isJPG = file.type === "image/jpeg";
  52. // const isIcon = file.type === "image/x-icon";
  53. // const ispng = file.type === "image/png";
  54. // const isLt2M = file.size / 1024 / 1024 < 3;
  55. // if (!isJPG && !ispng && !isIcon) {
  56. // this.$message.error("上传图片只能是 jpg/png/ico 图片格式!");
  57. // return false;
  58. // }
  59. // if (!isLt2M) {
  60. // this.$message.error("上传图片大小不能超过 3MB!");
  61. // return false;
  62. // }
  63. if (this.limtSize) {
  64. return new Promise((resolve, reject) => {
  65. let reader = new FileReader();
  66. reader.onload = e => {
  67. let data = e.target.result;
  68. //加载图片获取图片真实宽度和高度
  69. let image = new Image();
  70. image.onload = () => {
  71. let width = image.width;
  72. let height = image.height;
  73. // if (width != this.imgWidth || height != this.imgHeight) {
  74. // this.$message.error(`请上传${this.imgWidth}*${this.imgHeight}大小的图片`);
  75. // reject();
  76. // } else {
  77. // resolve();
  78. // }
  79. resolve();
  80. };
  81. image.src = data;
  82. };
  83. reader.readAsDataURL(file);
  84. });
  85. }
  86. },
  87. handleAvatarSuccess(res, file) {
  88. if (res.Ret == 200) {
  89. this.$message.success(res.Msg);
  90. this.imgUrl = res.Data;
  91. }
  92. }
  93. },
  94. watch: {
  95. imgUrl() {
  96. this.$emit("update:imgUrl", this.imgUrl);
  97. this.$emit("loadData");
  98. }
  99. }
  100. };
  101. </script>
  102. <style lang="scss" scoped>
  103. .avatar-uploader {
  104. display: inline-block;
  105. }
  106. .avatar-uploader .el-upload {
  107. border: 1px dashed #d9d9d9;
  108. border-radius: 6px;
  109. cursor: pointer;
  110. position: relative;
  111. overflow: hidden;
  112. width: 200px;
  113. height: auto;
  114. }
  115. .avatar-uploader .el-upload:hover {
  116. border-color: #409eff;
  117. }
  118. .avatar-uploader-icon {
  119. font: 28px/90px "微软雅黑";
  120. color: #8c939d;
  121. min-width: 200px;
  122. height: 90px;
  123. text-align: center;
  124. border: 1px dotted #ccc;
  125. }
  126. .avatar {
  127. width: 200px;
  128. height: auto;
  129. display: block;
  130. border: 1px solid #e3e3e3;
  131. }
  132. </style>