上传图片的demo

<template>
  <div>
    <el-form ref="configForm"
             :model="configForm"
             :rules="rules"
             class="configForm"
             label-width="128px">
      <el-form-item label="手绘地图:">
        <div class="upload el-upload-list--picture-card">
          <!-- 图片上传区 -->
          <el-upload v-if="!configForm.mapLink"
                     ref="uploadLogo"
                     class="avatar-uploader"
                     accept=".jpg, .png"
                     :action="upload.url"
                     :data="upData"
                     :auto-upload="false"
                     :show-file-list="false"
                     :limit="1"
                     :name="upload.name"
                     :on-change="handleChange"
                     :on-success="handleSuccess"
                     :before-upload="beforeUpload">
            <i class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
          <!-- 图片预览区 -->
          <div v-if="configForm.mapLink"
               class="pic-preview el-upload-list__item">
            <img :src="configForm.mapLink"
                 alt="手绘地图">
            <span class="el-upload-list__item-actions">
              <span class="el-upload-list__item-preview"
                    @click="handlePictureCardPreview()">
                <i class="el-icon-zoom-in"></i>
              </span>
              <span class="el-upload-list__item-delete"
                    @click="handleRemove()">
                <i class="el-icon-delete"></i>
              </span>
            </span>
          </div>
          <p class="upload-tips">
            说明:支持jpg、png格式、大小尽量控制在30MB内,上次的图片压缩质量越小,页面越顺畅
          </p>
        </div>
      </el-form-item>
    </el-form>
    <el-dialog :visible.sync="dialogVisible"
               :close-on-click-modal="false">
      <img :src="dialogImageUrl"
           alt="默认logo">
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'GISMapConfigInfo',
  data() {
    return {
      upload: {
        url: '//file.geeker.com.cn/uploadOSS',
        key: '',
        path: 'cloud-platform',
        name: 'Filedata'
      },
      dialogVisible: false,
      dialogImageUrl: '',
      configForm: {
        mapLink: ''
      },
      upData: {
        path: '',
        key: ''
      },
      rules: {}
    };
  },
  methods: {
    // 上传前
    beforeUpload(file) {
      const isJPG = (file.type === 'image/jpeg' || file.type === 'image/png');
      const isLt200K = file.size / 1024 <= 30720;
      if (!isJPG) {
        this.$message.error('提示', '手绘地图只能上传jpg、png格式图片');
        return false;
      }
      if (!isLt200K) {
        this.$message.error('提示', '手绘地图上传大于30M,请修改后重新上传');
        return false;
      }
      return true;
    },
    // 上传成功
    handleSuccess(res) {
      console.log(22222);
      if (res.state === 0 && res.safeUrl) {
        this.configForm.mapLink = res.safeUrl;
      } else {
        this.$message.error('提示', res.message || '上传失败');
      }
      // 上传完成后清除缓存
      this.$refs.uploadLogo.clearFiles();
      this.configForm = Object.assign({}, this.configForm);// 有可能直接赋值视图不更新,因此深拷贝一份
    },
    asyncImgChecked(file, params) {
      return new Promise((resolve) => {
        const reader = new FileReader();
        // 必须用file.raw,将图片转换成base64格式
        reader.readAsDataURL(file.raw);
        // 让页面中的img标签的src指向读取的路径
        reader.onload = () => {
          const img = new Image();
          img.src = reader.result;
          img.onload = () => {
            if (params.type === 'ico') {
              if (img.width !== params.width && img.height !== params.height) {
                this.$message.error('提示', '图片尺寸不合规格,请修改后重新上传!');
                // 上传完成后清除缓存
                this.$refs.uploadIcon.clearFiles();
                resolve(false);
              } else {
                resolve(true);
              }
            } else if (params.type !== 'ico') {
              resolve(true);
            }
          };
        };
      });
    },
    // onchange函数
    handleChange(file) {
      this.asyncImgChecked(file, {
        type: 'image'
      }).then((data) => {
        if (data) {
          this.$refs.uploadLogo.submit();
        }
      });
    },

    // 点击预览图片
    handlePictureCardPreview() {
      this.dialogImageUrl = this.configForm.mapLink;
      this.dialogVisible = true;
    },
    // 点击删除上传的图片
    handleRemove() {
      this.configForm.mapLink = '';
    }
  }
};
</script>

<style lang="scss" scoped>


 /deep/ .el-form-item__content {
        /* 上传图片样式 */
        .upload {
          .avatar-uploader .el-upload {
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            cursor: pointer;
          }
          .avatar-uploader .el-upload:hover {
            border-color: crimson;
          }
          .avatar-uploader-icon {
            width: 178px;
            height: 178px;
            color: #8c939d;
            font-size: 28px;
            line-height: 178px;
            text-align: center;
          }
          .avatar-uploader-icon:hover {
            color: crimson;
          }
          /* 图片预览样式 */
          .pic-preview {
            display: flex;
            align-content: center;
            justify-content: center;
            width: 178px;
            height: 178px;
            background-color: salmon;
            img {
              max-width: 100%;
              align-self: center;
            }
          }
        }
        .upload-tips {
          display: flex;
          align-items: flex-end;
          color:crimson;
          font-size: 14px;
        }
      }
</style>

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值