ElementUI上传图片400错误解决

问题描述:图片上传功能包括三个字段,1.图片名称,2.图片是否显示状态,3.上传的图片本身
方法一.使用before-upload完成上传操作
<el-upload ref="upload" action="012" :before-upload="beforeBannerUpload" n-change="imgChange" :auto-upload="false" name="file" accept="image/gif, image/jpeg, image/png" :show-file-list="false">
      <div class="bannerPic">轮播图片:</div>
      <el-button slot="trigger">选择上传图片</el-button>
      <div ref="fileList" class="fileList" v-if="file.name">{{file.name}}</div>
      <div class="fail-upload_tip" v-if="this.tipMessage">{{tipMessage}}</div>
      <div class="tip-message">备注:图片只能上传.png/.jpg/.gif格式,图片不能超过1M。</div>
      <div class="bannerAddOps">
        <el-button type="primary" @click="uploadImg">提交</el-button>
        <el-button type="primary" @click="cancel">返回</el-button>
      </div>
</el-upload>


beforeBannerUpload(file){
      let param = new FormData();
      param.append('file', file);
      param.append('title',this.bannerData.title);
      param.append('showState',this.bannerData.showState);
      this.bannerUpload(param);
    },
    async bannerUpload(param){
      try{
        await this.bannerUploadAction();
      }catch(err){
        this.$refs.upload.clearFiles();
        this.tipMessage = "上传图片失败";
      }
    },
    bannerUploadAction(){
      const url = '/plug-operation-mgmt/banner/add';
      const config = { reqInterceptors: true, resInterceptors: true };
      this.$axiosPost(url,param,config).then((res)=>{
        this.$refs.upload.clearFiles();

     });
   }
存在的问题是:默认的action还是会触发,会出现400错误,但不影响功能。

方法二:ElementUI上传控件有默认的参数可以试用
<el-upload ref="upload" action="http://localhost:8080/plug-operation-mgmt/banner/add" n-change="imgChange:auto-upload="falsename="file" :data="bannerDataaccept="image/gif, image/jpeg, image/png" :show-file-list="false">
      <div class="bannerPic">轮播图片:</div>
      <el-button slot="trigger">选择上传图片</el-button>
      <div ref="fileList" class="fileList" v-if="file.name">{{file.name}}</div>
      <div class="fail-upload_tip" v-if="this.tipMessage">{{tipMessage}}</div>
      <div class="tip-message">备注:图片只能上传.png/.jpg/.gif格式,图片不能超过1M。</div>
      <div class="bannerAddOps">
        <el-button type="primary" @click="uploadImg">提交</el-button>
        <el-button type="primary" @click="cancel">返回</el-button>
      </div>
    </el-upload>
其中bannerData中就是额外的参数图片名称和显示状态,直接触发默认action就可以了。其中name="file"中file为服务端接口中指定文件的名称

转载于:https://www.cnblogs.com/haojf/p/9834826.html

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
作为AI语言模型,我无法进行代码编写和图形界面操作。但我可以为您提供ElementUI上传图片的基本步骤: 1. 引入ElementUI组件库和相关样式文件。 ``` import Vue from 'vue' import ElementUI from 'element-ui' import 'element-ui/lib/theme-chalk/index.css' Vue.use(ElementUI) ``` 2. 在HTML模板中添加上传组件。 ``` <el-upload class="upload-demo" action="/upload" :on-success="handleSuccess" :before-upload="beforeUpload" :on-error="handleError" :file-list="fileList" :auto-upload="false" :multiple="true"> <el-button slot="trigger" size="small" type="primary">选取文件</el-button> <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上传到服务器</el-button> <div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div> </el-upload> ``` 3. 在Vue组件中定义上传相关的方法。 ``` export default { data() { return { fileList: [] } }, methods: { beforeUpload(file) { const isJPG = file.type === 'image/jpeg' || file.type === 'image/png' const isLt2M = file.size / 1024 / 1024 < 2 if (!isJPG) { this.$message.error('上传图片只能是 JPG/PNG 格式!') return false } if (!isLt2M) { this.$message.error('上传图片大小不能超过 2MB!') return false } return true }, handleSuccess(response, file, fileList) { this.$message.success('上传成功') }, handleError(error, file, fileList) { this.$message.error('上传失败') }, submitUpload() { this.$refs.upload.submit() } } } ``` 以上是ElementUI上传图片的基本步骤,您可以根据具体的业务需求进行相应的调整和修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值