点击上传按钮,直接弹出文件选择

     <el-upload
        style="display: inline; margin-left: 20px"
        class="upload-demo"
        ref="upload"
        :action="upLoadUrl"
        accept=".docx"
        :show-file-list="false"
        :headers="uploadheaders"
        :before-upload="beforeUpload"
        :on-success="handle_success"
        :data="upLoadText"
      >
        <!-- <el-button type="danger" @click="viewFile" icon="el-icon-view"
            >预览</el-button
          > -->
      </el-upload>
      <el-button slot="trigger" type="primary" @click="upLoadFile"
        >上传</el-button
      >
data() {
  return {
  	  uploadheaders: {
        Authorization: "",
      },
       upLoadText: {},
        addForm: {
        file: null,
        fileName: "",
        fileData: null,
      },
  }
},
computed: {
 upLoadUrl() {
      return process.env.VUE_APP_BASE_API + "/portal-file/upload";
    },
},
methods: {
	 //上传之前调用方法
    beforeUpload(file) {
      this.uploadheaders.Authorization = "Bearer " + getToken();
      this.addForm.file = file;
      this.addForm.fileName = file.name;
      // this.fileSize = file.size;
      const extension = file.name.split(".").slice(-1) == "docx";
      if (!extension) {
        this.$message.warning("上传模板只能是word格式!");
        return;
      }
      let reader = new FileReader();
      reader.readAsDataURL(file);
      let that = this;
      reader.onload = function () {
        that.addForm.fileData = reader.result;
      };
      return true; // 返回false不会自动上传
    },
   // 上传成功后
    handle_success(res) {
      console.log("上传成功:", res);
      this.query.sourcePath = res.data.url;
      this.query.sourceName = res.data.name;
      console.log("query:", this.query);
      upload(this.query).then((res) => {
        // console.log("上传后:", res);
        this.$message({
          message: "上传成功",
          type: "success",
        });
        this.query.id = "";
      });
    },
 upLoadFile() {
      if (this.query.id) {
        this.toSelectFile();
        this.search();
      } else {
        this.$message({
          message: "请选择要上传文件的模板",
          type: "warning",
        });
      }
    },
    toSelectFile() {
      this.upLoadText = {
        creator: window.sessionStorage.getItem("accountName"),
        fileName: this.addForm.fileName,
        requirement: this.addForm.selectedRequirement,
      };
      // 触发upload组件内部点击事件,弹出文件选择框
      this.$refs["upload"].$refs["upload-inner"].handleClick();
      console.log(this.addForm);
    },
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值