VUE elementUI upload图片上传组件demo

<template>
  <div class="upload-container">
    <el-upload
      :data="dataObj"
      multiple
      :limit="limit"
      :list-type="listType"
      :show-file-list="true"
      class="image-uploader"
      drag
      :on-exceed="outLimit"
      :file-list="fileList"
      :auto-upload="true"
      :http-request="uploadSectionFile"
      action="123"
    >
      <i class="el-icon-upload" />
      <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
    </el-upload>
  </div>
</template>

<script>
import { uploadpic } from "@/api/xy_fileupload";
import { getToken } from "@/utils/auth";
export default {
  name: "SingleImageUpload",
  props: {
    limit: { type: Number, default: 1 }, //限定数量
    listType: { type: String, default: "text" } //限定展示方式 text/picture/picture-card
  },
  data() {
    return {
      fileList: [],
      tempUrl: "",
      dataObj: { Authorization: "" }
    };
  },
  mounted() {
    this.init();
  },
  methods: {
    uploadSectionFile(param) {
      var fileObj = param.file;
      // FormData 对象
      var form = new FormData();
      // 文件对象
      form.append("file", fileObj);
      let _this = this;
      uploadpic(form).then(res => {
        if (res.code === 10000) {
          _this.value = res.picUrl;
          // 上传成功
          _this.$emit("uploadSuccess", res.picUrl);
        } else {
          // 上传失败
          _this.$emit("error", res.message);
        }
      });
    },
    init() {
      this.dataObj.Authorization = getToken();
    },
    //文件数量超出限制
    outLimit() {
      // this.$emit("outLimit","文件超出限制" );
      this.$message({
        message: "文件数量超出限制",
        type: "warning"
      });
    },
    rmImage() {
      this.emitInput("");
    }
  }
};
</script>

<style lang="scss" scoped>
@import "~@/styles/mixin.scss";
.upload-container {
  width: 100%;
  position: relative;
  @include clearfix;
  .image-uploader {
    width: 100%;
    float: left;
  }
  .image-preview {
    width: 10%;
    height: 10%;
    position: relative;
    border: 1px dashed #d9d9d9;
    float: left;
    margin-left: 50px;
    .image-preview-wrapper {
      position: relative;
      width: 100%;
      height: 100%;
      img {
        width: 100%;
        height: 100%;
      }
    }
    .image-preview-action {
      position: absolute;
      width: 100%;
      height: 100%;
      left: 0;
      top: 0;
      cursor: default;
      text-align: center;
      color: #fff;
      opacity: 0;
      font-size: 20px;
      background-color: rgba(0, 0, 0, 0.5);
      transition: opacity 0.3s;
      cursor: pointer;
      text-align: center;
      line-height: 200px;
      .el-icon-delete {
        font-size: 36px;
      }
    }
    &:hover {
      .image-preview-action {
        opacity: 1;
      }
    }
  }
}
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值