element-ui 上传图片,限制图片的格式和大小

<el-form-item label="图片:" prop="tempImagePath">
  <el-upload
    class="upload"
    accept="image/jpeg"
    :show-file-list="false"
    list-type="picture-card"
    :headers="{ token: token}"
    :action="actionUrl"
    :before-upload="beforeAvatarUpload"
    :on-success="handleAvatarSuccess">
    <img v-if="temp.tempImagePath"
         :src="temp.tempImagePath" width="146px" height="146px"/>
    <i v-else class="el-icon-plus"></i>
    <div slot="tip" class="el-upload__tip">
      只能上传.gif/.jpeg/.png文件且小于500K
    </div>
  </el-upload>
</el-form-item>

 

beforeAvatarUpload (file) {
  const imgType = file.type === 'image/jpeg' || file.type === 'image/png'
  const isLt500k = file.size / 1024 / 1024 < 0.48;
  if (!imgType) {
    this.$message.error("上传图片只能是 JPG和png 格式!");
    return false;
  }
  if (!isLt500k) {
    this.$message.error("上传图片大小不能超过 500k!");
    return false;
  }
},

Logo

前往低代码交流专区

更多推荐