使用el-upload组件覆盖默认的上传行为,可以自定义上传的实现(就是将默认的post方式改成get或者其他)

//vue模板中的样式     
 <el-form-item label="选择文件:" prop="filepath">
          <el-upload
            class="upload-demo"
            action=""
            :on-remove="handleRemove"
            :before-remove="beforeRemove"
            :file-list="fileList"
            :limit="limitNum1"
            :http-request="getFangShi"
            accept=".xls,.xlsx"
          >
            <!-- :show-file-list="false" -->
            <el-button size="small" type="primary">点击上传文件</el-button>
          </el-upload>
        </el-form-item>

//data中需要定义的数据字段
 // 上传附件用
      fileList: [],
      limitNum1: 1, //只允许上传一个附件-----后端说按照一个来先

//js写法
getFangShi(item) {
      let formData = new FormData();
      formData.append("file", item.file);
      console.log(formData.get("file"), "这是给文件传入时候的数据append");
      formData.append("rwid", this.dataId);
      console.log(formData.get("rwid"), "append之后的数据");
      axios({
        method: "post",
        url: this.$config + "/qycqgz/importData",
        data: formData,
        headers: {
          "Content-Type":
            "multipart/form-data;boundary = " + new Date().getTime(),
        },
      })
        .then((res) => {
          console.log(res, "成功");
        })
        .catch(function (error) {
          console.log(error, "失败");
        });
    },
handleRemove(file, fileList) {
      console.log(file, fileList);
      this.addForm.filepath = "";
      this.addForm.filename = "";
      this.fileList = [];
    },
 beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}?`);
    },

其主要用到了http-request方法,成功(on-success)和失败(on-error)的函数可以不写

### Element UI `el-upload` 组件文件上传完整代码示例 为了实现通过 `el-upload` 组件上传整个文件夹的功能,可以按照如下方式构建完整的 Vue文件组件 (SFC): #### HTML 部分 ```html <template> <div class="upload-container"> <el-upload ref="upload" :auto-upload="false" :on-change="handleChange" :http-request="customUpload" multiple directory action="" accept=".zip,.rar,.7z" list-type="text" > <el-button type="primary">点击上传</el-button> </el-upload> <el-button @click="submitUpload" style="margin-top: 20px;" type="success">提交上传</el-button> </div> </template> ``` 此部分定义了一个带有自定义按钮样式的上传区域,并允许用户选择多个文件或目录。 #### JavaScript 部分 ```javascript <script> export default { data() { return { fileList: [], // 存储选中的文件列表 formData: new FormData(), // 创建表单数据对象用于后续处理 }; }, methods: { handleChange(file, fileList) { this.fileList = fileList; console.log('Selected files:', file.raw); if (!this.formData.has('files')) { this.formData.append('files', []); } const filesArray = this.formData.get('files'); filesArray.push(file.raw); this.formData.set('files', filesArray); }, customUpload(options) { // 自定义上传逻辑可在此处编写 console.log('Custom Upload Options:', options); }, submitUpload() { if(this.fileList.length === 0){ alert("请选择要上传文件"); return false; } let config = { headers: {'Content-Type': 'multipart/form-data'} }; axios.post('/api/upload', this.formData, config).then(response => { console.log('Files uploaded successfully!'); }).catch(error => { console.error('Error during upload:', error); }); } } }; </script> ``` 这段脚本实现了当用户选择了新的文件时更新本地状态以及准备发送的数据;还提供了手动触发实际HTTP请求的方法来完成最终的服务器端传输操作。 #### CSS 样式调整 如果希望在某些条件下隐藏上传按钮,则可以根据需求应用特定类名并修改对应的选择器规则: ```css <style scoped> /* 默认显示 */ .el-upload--picture-card{ width:110px; height:110px; line-height:110px; } /* 条件满足时隐藏 */ .disUoloadSty .el-upload--picture-card{ display:none !important; /* 使用!important强制覆盖其他样式 */ } </style> ``` 上述代码片段展示了如何利用Element UI框架下的`el-upload`插件实现基本功能的同时支持文件夹形式的内容选取与上载过程[^1]。请注意,在真实项目环境中还需要考虑安全性验证等问题,并确保API接口能够正确接收来自前端传递过来的信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值