简单谈谈el-upload文件上传问题

1.需求因为先不要上传服务器,而是选中文件,最后以FormData形式传递给后台
在这里插入图片描述

2.说一下el-upload的属性与方法,也是解题的关键

action=""是指上传莫某个服务器,我这里没用直接上传就用#表示
accept=“.apk” 限制选择文件的类型。因为我这里是还没上传服务器就要提前校验,用不了before-upload
on-preview 是点击文件返回的信息,这里没用到
on-remove 是删除文件返回的信息
multiple 是否支持多选文件,布尔类型
on-change 是文件状态改变时候的返回file信息,获取Files关键方法
before-upload 上传文件之前的钩子函数,一般做直接上传服务器前的校验限制
auto-upload 是否在选取文件后立即进行上传,对于不上传action的非常关键
file-list 上传的文件列表, 例如: [{name: ‘food.jpg’, url:
limit 最大允许上传个数
on-exceed 文件超出个数限制时的钩子

3.代码图下实现,主要是通过on-change事件获取到文件File

 <el-form-item
                        :label="$t('manage.SWPkG') +''"
                        :label-width="formLabelWidth"
                        class="fileupdown"
                    >
                        <el-input
                            placeholder="no file ..."
                            style="width: 76%"
                            class="input-with-select"
                        >
                            <el-upload
                                class="upload-demo"
                                slot="append"
                                action="#"
                                :on-change="handleChangePic"
                                :on-remove="handleRemoveApk"
                                accept=".apk"
                                :file-list="fileList"
                                :auto-upload="false"
                            >
                                <el-button
                                    slot="trigger"
                                    size="small"
                                    type="success"
                                >choose apk file</el-button>
                            </el-upload>

                        </el-input>
                    </el-form-item>
                    <el-form-item class="fileupdown">
                        <el-input
                            placeholder="no file ..."
                            style="width: 76%"
                            class="input-with-select"
                        >
                            <el-upload
                                class="upload-demo"
                                slot="append"
                                action="#"
                                :on-change="handleChangeTxt"
                                :on-remove="handleRemoveTxt"
                                accept=".txt"
                                :file-list="fileLists"
                                :auto-upload="false"
                            >
                                <el-button
                                    slot="trigger"
                                    size="small"
                                    type="success"
                                >choose text file</el-button>
                            </el-upload>

                        </el-input>
                    </el-form-item>
 handleChangePic(file, fileList) {
      if (fileList.length > 1) {
        fileList.splice(0, 1);
      }
      this.apkFiles = fileList[0].raw;
    },
    handleChangeTxt(file, fileList) {
      if (fileList.length > 1) {
        fileList.splice(0, 1);
      }
      this.txtFiles = fileList[0].raw;
    },
    handleRemoveApk(file, fileList) {
      console.log(file, fileList);
      this.apkFiles = {};
    },
    handleRemoveTxt(file, fileList) {
      console.log(file, fileList);
      this.txtFiles = {};
    },
    confirmDialog() {
      this.$refs["versionForm"].validate(valid => {
        if (valid) {
          if (
            !this.versionForm.number ||
            !this.versionForm.numberString ||
            !this.versionForm.describe ||
            JSON.stringify(this.apkFiles) === "{}" ||
            JSON.stringify(this.txtFiles) === "{}"
          ) {
            this.$alert(
              this.$t("doctor.completeData"),
              this.$t("doctor.message"),
              {
                confirmButtonText: this.$t("home.yes"),
                callback: () => {}
              }
            );
            return;
          }
          this.formData = new FormData();
          this.formData.append("version", this.versionForm.number);
          this.formData.append("versionName", this.versionForm.numberString);
          this.formData.append("des", this.versionForm.describe);
          this.formData.append("apk", this.apkFiles);
          this.formData.append("mapper", this.txtFiles);

          this.$confirm(this.$t("doctor.confirmSave"), this.$t("home.remind"), {
            confirmButtonText: this.$t("home.yes"),
            cancelButtonText: this.$t("home.cancel"),
            type: "warning"
          })
            .then(() => {
              api.uploadApk(this.formData).then(res => {
                if (res.code == 0) {
                  this.isdisplayDialog = false;
                  this.$alert(res.msg, this.$t("doctor.message"), {
                    confirmButtonText: this.$t("home.yes"),
                    callback: action => {
                      return;
                    }
                  });
                  this.$emit("addgetList");
                } else {
                  this.$message({
                    type: "error",
                    message: res.msg
                  });
                }
              });
            })
            .catch(() => {});
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    }
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
el-upload 是饿了么团队开发的一个 Vue.js 文件上传组件,可以方便地在前端实现文件上传功能。使用 el-upload 组件,你只需要在 Vue.js 模板中添加一个 el-upload 标签,并设置相应的属性,就可以实现文件上传功能。 下面是一个使用 el-upload 组件实现文件上传的示例代码: ```html <template> <div> <el-upload class="upload-demo" action="/upload" :on-success="handleSuccess" :before-upload="beforeUpload" :headers="headers" :limit="3" :data="{foo: 'bar'}" :accept="'image/*'"> <el-button slot="trigger" size="small" type="primary">选取文件</el-button> <div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div> </el-upload> </div> </template> <script> export default { data() { return { headers: { Authorization: 'Bearer ' + localStorage.getItem('token') } } }, methods: { handleSuccess(response, file, fileList) { console.log(response) }, 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 格式!') } if (!isLt2M) { this.$message.error('上传图片大小不能超过 2MB!') } return isJPG && isLt2M } } } </script> ``` 在上面的代码中,我们设置了 el-upload 的 action 属性来指定上传文件的地址,headers 属性来指定上传文件时需要发送的请求头,before-upload 属性来指定上传文件前需要进行的验证,limit 属性来指定上传文件的数量限制,data 属性来指定上传文件时需要发送的数据,accept 属性来指定上传文件的类型限制。同时,我们还设置了 el-upload 的 trigger 和 tip 插槽,用来自定义上传按钮和提示信息。 需要注意的是,el-upload 组件需要配合后端进行使用,后端需要提供相应的接口来接收上传文件

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值