Element 单独上传图片,不和form表单一起

1 Html

          <el-form-item label="图片" prop="picUrl" :label-width="formLabelWidth">

            <el-upload

              v-model="form.picUrl"    // 表单提交对象

              :action="actionImg"  // 图片上传地址,

              list-type="picture-card"

              :on-success="successImg"  //图片上传完成的回调

              auto-upload

              :headers="headers"  // 请求头

              ref="upload">

              <i class="el-icon-plus"></i>

            </el-upload>

          </el-form-item>

2 Js

  // 图片上传的 请求头

  computed: {

    headers() {

      return {

        Authorization: window.sessionStorage.getItem("token")

      };

    }

  }

    // 图片上传完成的回调

    successImg(response, file, fileList) {

      if (response) {  // 按各自后端需要来写,图片数据都在response里面

        this.form.picUrl = response.data.url;  

        this.form.remotePath = response.data.remotePath; 

      }

    },

    // 确定按钮

    addSubmit(from) {

      this.$refs[from].validate(async valid => {

        if (valid) { 

          if (this.form.picUrl == "") { //判断图片有没有上传成功

            this.$message.error("请选择图片");

            return false;

          }

          // 成功 提交数据

          let res = await this.$axios.post("/product/save", this.form);

          if (res.data.code != 0) {

            this.$message.error(res.data.message);

          } else if (res.data.code == 0) {

            this.$message.success("新增成功");

          }

        }

      });

    },

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容