Vue+Elementui的el-upload实现文件上传

项目场景:

大数据中台


问题描述

导入文件二进制流


解决方案:

写法一:

   <el-upload
	     ref="upload1"
	     class="upload-demo"
	     action="rest/v2/gonghu_schema_zdy/..." // 提交的地址
	     :data="{ graph: this.enName }" // 要传的参数
	     :on-success="fileSuccess" // 绑定提示方法
	     accept=".txt"
	     :before-upload="beforeAvatarUpload"
	     :show-file-list="false"
	 >
	     <p @click="leadingIn" class="to">导入概念三元组</p >
	</el-upload>

methods中写提示

 //概念 txt上传成功
 fileSuccess() {
      this.$message({
          message: "概念三元组上传成功!",
          type: "success",
      });
  },

效果:
在这里插入图片描述


写法二:

  • 控制台打印的上传文件格式
    在这里插入图片描述
  • 传给后端的格式
    在这里插入图片描述

代码:

<el-upload
  ref="upload2"
  class="upload-demo"
  action
  accept=".ttl"
  :http-request="httpRequestTxt"
  :before-upload="beforeFileTtl"
  :show-file-list="false"
>
  <p @click="leadingIn" class="to">导入概念三元组owl</p>
</el-upload>

methods:{
	httpRequestTxt(param) {
	  console.log(param);
	  if (this.flag) {
	    let file = param.file;
	    let formData = new FormData();
	    formData.append("fileName", file);
	    let user = this.$store.state.variable.userData;
	    importConceptSYZByOwl(this.enName, this.loginName, user, formData).then(
	      (res) => { 
	        if (res.data.status == "SUCCESS") {
	          this.$Message.success("导入成功");
	          this.getConstructList();
	          this.visible = false;
	        } else if (res.data.code == 500) {
	          this.$Message.error(res.data.result);
	        }
	      }
	    );
	  }
	},
	//上传前的校验
	beforeFileTtl(file) {
	  const fileName = file.name;
	  const fileType = fileName.substring(fileName.lastIndexOf("."));
	  if (fileType === ".ttl") {
	    this.flag = true;
	  } else {
	    this.$Message.error("上传类型错误,请选择ttl文件");
	    return false;
	  }
	},
}

写法三:

  • 控制台打印的上传文件格式
    在这里插入图片描述

  • 传给后端的格式
    在这里插入图片描述

代码:

<el-upload
  class="upload-demo"
  action
  :limit="1"
  :file-list="fileList"
  :on-change="handleChange"
  :show-file-list="true"
  :on-remove="handleRemove"
  :before-remove="beforeRemove"
  :auto-upload="false"
>
  <Button type="primary">选取文件</Button>
</el-upload>

methods:{
    handleChange(file) {
      console.log(file);
      let user = this.$store.state.variable.userData; 
      let type = file.name.split(".")[1];
      if (type == "pmml") {
        this.fileType = "pmml";
        this.modelId = Date.now();
        let fileRaw = file.raw;
        let fd = new FormData();
        fd.append("file", fileRaw);
        importModel(fd, this.modelId, user).then((res) => {
          if (res.data == "fail") {
            this.$Message.error("上传失败!");
            this.fileList = [];
          }
        });
      } else {
        this.$Message.error("格式错误,请上传pmml格式!");
        this.fileList = [];
      }
    },
    //文件移除
    handleRemove(file, fileList) {},
    beforeRemove(file) {
      return this.$confirm(`确定移除 ${file.name}?`);
    },

}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值