vue使用el-upload上传,上传错误文件,名称还会显示

vue使用el-upload上传,上传错误文件,名称还会显示

背景:上传不符合后缀的文件,但是还是在页面中显示,使用的是http-request方法;上传文件有两种方式:action和http-request

代码:

<el-upload multiple class="avatar-uploader" action="#" :before-upload="(e, fileList) => {beforeUpload(e, fileList, scope.row, scope.$index)}"  :file-list="scope.row.fileList"  :on-remove="handleRemove()" :before-remove="(e, fileList) => {beforeRemove(e, fileList, scope.row, scope.$index)}" :http-request="(e, fileList) => {handleChange(e, fileList, scope.row, scope.$index)}">
  <el-button style="background-color: #fff; color: gray; border-color: gray;"  size="small" icon="el-icon-circle-plus-outline" type="primary"></el-button>
</el-upload>
// 限制上传文件格式
      limitChange(file, row) {
        var limitFile = true;
        var msg = "";
       //获取文件后缀
        const fileSuffix = file.name.substring(file.name.lastIndexOf(".") + 1);
        if (row.templateType === 1) {
          // 上传图片jpg、png
          const whiteList = ["jpg", "png"];
          if (whiteList.indexOf(fileSuffix) === -1) {
            this.$message.error('上传图片只能是jpg、png格式');
            limitFile = false;
          }
        } else if (row.templateType === 2) {
          // 上传压缩文件 zip、rar
          const whiteList = ["zip", "rar"];
          if (whiteList.indexOf(fileSuffix) === -1) {
            this.$message.error('上传图片只能是zip、rar格式');
            limitFile = false;
          }

        } else if (row.templateType === 3) {
          // 上传音视频 mp3、mp4
          const whiteList = ["mp3", "mp4"];
          if (whiteList.indexOf(fileSuffix) === -1) {
            this.$message.error('上传图片只能是mp3、mp4格式');
            limitFile = false;
          }
        } else if (row.templateType === 5) {
          // 上传文档:pdf、doc、docx、ppt、pptx
          const whiteList = ["pdf", "doc", "docx", "ppt", "pptx"];
          if (whiteList.indexOf(fileSuffix) === -1) {
            this.$message.error('上传图片只能是pdf、doc、docx、ppt、pptx格式');
            limitFile = false;
          }
        } else if (row.templateType === 6) {
          // CAD:dwg,dfx
          const whiteList = ["dwg", "dfx"];
          if (whiteList.indexOf(fileSuffix) === -1) {
            this.$message.error('上传文件只能是dwg、dfx格式');
            limitFile = false;
          }
        } else if (row.templateType === 7) {
          // 三维模型:skp,max,fbx
          const whiteList = ["skp", "max", "fbx"];
          if (whiteList.indexOf(fileSuffix) === -1) {
            this.$message.error('上传文件只能是skp、max, fbx格式');
            limitFile = false;
          }
        }
        return limitFile;
       
      },
      // 上传附件,有时间和上传图片一起封装成一个方法
      handleChange(e, fileList, row, index) {

        var flag = this.limitChange(e.file, row);
        if (!flag) {
        // 主要是这一句
          e.onError()
          return;
        }
        var vm = this
        let file = e.raw
        ...
   }
Vue使用el-upload组件来上传图片的步骤如下: 1. 在Vue组件中引入el-upload组件,并添加相应的props和data属性。 2. 在模板中使用el-upload标签,并设置相应的属性,例如action属性指定图片上传的接口地址,list-type属性指定展示上传图片的样式等。 3. 在methods中定义handleAvatarSuccess方法作为上传成功的回调函数,通过该方法获取服务端返回的图片路径,并将其绑定到imageUrl属性上。 4. 在methods中定义beforeAvatarUpload方法作为上传前的验证方法,用于验证图片格式和大小是否符合要求。 5. 在methods中定义handleRemove方法作为删除图片的回调函数,通过该方法从imageList中移除对应的图片对象。 6. 在methods中定义handlePictureCardPreview方法作为图片预览的回调函数,通过该方法设置预览图片的相关属性。 7. 在模板中使用v-if和v-else指令来根据是否有图片路径来展示或隐藏图片。 以上就是使用el-upload组件在Vue上传图片的基本步骤。请根据实际情况进行相应的配置和调整。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [vue使用element-ui的el-upload上传图片至服务器,服务端使用的是node.js对接受的图片进行处理](https://blog.csdn.net/qq_49042268/article/details/122722395)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [vue el-upload实现图片和文字上传](https://blog.csdn.net/weixin_47978760/article/details/128042608)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值