vue项目formdata格式上传文件

本文介绍在Vue项目中如何利用FormData对象实现文件上传功能,包括创建上传按钮和处理文件上传的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

按钮

		<el-upload class="upload-demo" action=""
            :on-preview="handlePreview" :on-remove="handleRemove" 
            :file-list="fileList" :limit="1" accept=".docx, .pdf, .doc"
            :on-exceed="handleExceed" :http-request="modeUpload">

            <el-button type="primary" icon="el-icon-upload2" size="small">
            上传文件
            </el-button>
         </el-upload>
		
		
<el-button type="primary" @click="submitFormFile" :disabled="isDisable">
确 定
</el-button>

上传

	modeUpload(item) {
      //console.log("222", item);
      this.mode = item.file;
    },
    handleRemove(file, fileList) {
      //console.log(file, fileList);
    },
    handlePreview(file) {
      //console.log(file);
    },
    //限制传文件个数
    handleExceed(files, fileList) {
      this.$message.warning(
        `当前限制选择 1 个文件,本次选择了 ${files.length} 个文件,共选择了 ${
        files.length + fileList.length
      } 个文件`
      );
    },
    //上传文件移除事件
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}`);
    },
/** 提交按钮 */
    submitFormFile() {
      this.$refs["form2"].validate((valid) => {
        if (valid) {

          if (this.form2.id != null) {
            this.isDisable=true
            updateFile(this.form2).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open2 = false;
                this.getListFile();
              }
            });
          } else {

            this.form2.archivesDataId = this.select.id;
            this.form2.archivesId = this.id;
            //console.log('2',this.form2)

            if (this.mode === null) {
              this.$message({
                showClose: true,
                message: "文件不能为空",
                type: "warning",
              });
            } else {
              this.isDisable=true
              let fd = new FormData();
              fd.append("file", this.mode);


              uploadArchivesFile(this.form2,fd).then((response) => {
                if (response.code === 200) {
                  this.msgSuccess("新增成功");
                  this.open2 = false;
                  this.getListFile();
                }
              });
            }


          }
        }
      });
    },
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值