1

HTML代码:

    <el-upload
        style="width:300px;"
      ref="upload"
      class="upload-demo"
        :action="actionPath"
        :headers="myHeaders"
        :data="form"
        :show-file-list="true"
        :on-change="changeFileList"
        :on-success="successMethod"
        :on-remove="removeHandle"
        :on-error="errorMethod"
        :multiple="true"
        :before-upload="beforeAvatarUpload">

js代码:

methods: {
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      if (!isJPG) {
          this.$confirm('上传图片暂时只支持JPG格式', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error' ,
            callback: action => {
              this.$refs.form.resetFields();

              this.$emit('closeEditUpload');
            }
          })
      }
      return isJPG;
    },
   }
   
 现在只能上传jpg格式,我想上传jpg和png,怎么限制???
        
夜雨 6
2017-11-09 提问

查看全部 4 个回答

1

accept="image/gif,image/jpeg,image/jpg,image/bmp,image/png"

不用谢,往哪儿写就不用我说了吧

推荐答案

3
        <el-upload class="upload-demo" :multiple="true" :action="action" accept="image/jpeg,image/gif,image/png,image/bmp" :file-list="fileList" :before-upload="beforeAvatarUpload" :on-success="handleAvatarSuccess">


beforeAvatarUpload(file) {

            this.imageFileName.push(file.name);
            const isJPG = file.type === 'image/jpeg';
            const isGIF = file.type === 'image/gif';
            const isPNG = file.type === 'image/png';
            const isBMP = file.type === 'image/bmp';
            const isLt2M = file.size / 1024 / 1024 < 2;

            if (!isJPG && !isGIF && !isPNG && !isBMP) {
                this.common.errorTip('上传图片必须是JPG/GIF/PNG/BMP 格式!');
            }
            if (!isLt2M) {
                this.common.errorTip('上传图片大小不能超过 2MB!');
            }
            return (isJPG || isBMP || isGIF || isPNG) && isLt2M;
        },

推广链接