• element-ui 图片上传组件封装


    <template>
      <div>
        <el-upload
          list-type="picture-card"
          class="avatar-uploader"
          :action="action"
          :auto-upload="true"
          :data="uploadData"
          name="file"
          :show-file-list="true"
          :on-success="handleSuccess"
          :before-upload="beforeUpload"
          :on-exceed="handleExceed"
          :on-remove="handleRemove"
          accept="image/png, image/jpeg, image/gif, image/jpg"
          :limit="1"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          <!-- remove图标没有起作用 -->
          <i slot="default" class="el-icon-plus"></i>
          <div slot="file" slot-scope="{file}">
            <!-- <img class="el-upload-list__item-thumbnail" :src="imageUrl" alt /> -->
            <span class="el-upload-list__item-actions">
              <span v-if="!disabled" class="el-upload-list__item-delete">
                <i class="el-icon-delete"></i>
              </span>
            </span>
          </div>
        </el-upload>
      </div>
    </template>
    <script>
    export default {
      data() {
        return {
          // action: process.env.VUE_APP_IMG_URL + "/image/upload",
          action: "http://img.tourdev.cn:8001/nv2/image/upload",
          disabled: false,
          uploadData: {
            file: "",
            project: "JINXINSERVE_WEB"
          },
          imageUrl: ""
        };
      },
      props: {
        coverUrl: {
          type: String,
          default: ""
        }
      },
      created() {
        this.imageUrl = this.coverUrl;
      },
      methods: {
        handleRemove(file) {
          this.$emit("getUrlFn", "");
        },
        beforeUpload(file) {
          this.uploadData.file = file;
          const isLt2M = file.size / 1024 / 1024 < 2;
          if (!isLt2M) {
            this.$message.error("上传头像图片大小不能超过 2MB!");
            return isLt2M;
          }
        },
        handleSuccess(res, file) {
          // this.imageUrl = URL.createObjectURL(file.raw);
          // 获取后端对上传图片存储位置的路径,
          if (res.code === 200) {
            // console.log("pic", res);
            this.imageUrl = "";
            // this.$emit("getUrlFn", res.url);
            this.$emit("getUrlFn", res.browser);
          } else {
            this.$message.error("图片上传失败");
          }
        },
        handleExceed(files, fileList) {
          this.$message.error("只能上传一张图片");
        }
      }
    };
    </script>
    <style scope>
    .avatar-uploader .el-upload {
      border: 1px dashed #d9d9d9;
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
    }
    .avatar-uploader .el-upload:hover {
      border-color: #409eff;
    }
    .avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
       148px;
      height: 148px;
      line-height: 148px;
      text-align: center;
    }
    .avatar {
       148px;
      height: 148px;
      display: block;
    }
    </style>

    //显示编辑图片

    <template>
      <div>
        <el-upload
          class="avatar-uploader"
          :action="action"
          :auto-upload="true"
          :data="uploadData"
          name="file"
          :show-file-list="false"
          :on-success="handleSuccess"
          :before-upload="beforeUpload"
          accept="image/png, image/jpeg, image/gif, image/jpg"
        >
          <!-- remove图标没有起作用 -->
          <div slot="file" slot-scope="{file}">
            <span class="el-upload-list__item-actions">
              <span v-if="!disabled" class="el-upload-list__item-delete" @click="handleRemove(file)">
                <i class="el-icon-delete"></i>
              </span>
            </span>
          </div>
          <img v-if="imageUrl" :src="imageUrl" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </div>
    </template>
    <script>
    export default {
      data() {
        return {
          action: "http://img.tourdev.cn:8001/nv2/image/upload",
          disabled: false,
          uploadData: {
            file: "",
            project: "JINXINSERVE_WEB"
          },
          imageUrl: ""
        };
      },
      props: {
        coverUrl: {
          type: String,
          default: ""
        }
      },
      created() {},
      watch: {
        coverUrl(val) {
          this.imageUrl = val;
        }
      },
      methods: {
        handleRemove(file) {
          console.log(file);
        },
    
        beforeUpload(file) {
          this.uploadData.file = file;
          const isLt2M = file.size / 1024 / 1024 < 2;
          if (!isLt2M) {
            this.$message.error("上传头像图片大小不能超过 2MB!");
            return isLt2M;
          }
        },
        handleSuccess(res, file) {
          // this.imageUrl = URL.createObjectURL(file.raw);
          // 获取后端对上传图片存储位置的路径,
          if (res.code === 200) {
            this.imageUrl = res.url;
            this.$emit("getUrlFn", this.imageUrl);
          } else {
            this.$message.error("图片上传失败");
          }
        }
      }
    };
    </script>
    <style scope>
    .avatar-uploader .el-upload {
      border: 1px dashed #d9d9d9;
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
    }
    .avatar-uploader .el-upload:hover {
      border-color: #409eff;
    }
    .avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
       148px;
      height: 148px;
      line-height: 148px;
      text-align: center;
    }
    .avatar {
       148px;
      height: 148px;
      display: block;
    }
    </style>
  • 相关阅读:
    SQL盲注工具BBQSQL
    嗅探X-Windows服务按键工具xspy
    多协议底层攻击工具Yesinia
    LLMNR欺骗工具Responder
    Arduino可穿戴教程保存源文件与打开已经存在的源文件
    GRDB使用SQLite的WAL模式
    CString之GetBuffer与ReleaseBuffer
    VC++ 模块与资源分离
    KV6.60 SP1
    Html之head部分详解
  • 原文地址:https://www.cnblogs.com/Byme/p/13130845.html
Copyright © 2020-2023  润新知