element UI的Upload 自定义上传

 1.组件布局

      
      <el-upload
        class="upload-demo"
        ref="upload"
        :headers="uploadFace.headers"
        :action="uploadFace.url"
        :auto-upload="false"
        :limit="1"
        :on-success="handleAvatarSuccess"
        :on-remove="handleRemove"
        :file-list="uploadFace.fileList"
        drag
        :http-request="handleFileUpload"
        accept=".jpg,.jpeg,.png,.pdf"
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
        <div class="el-upload__tip" slot="tip">
          (支持文件类型<span style="color: #65b1ff">.jpg,.jpeg,.png,.pdf</span
          >)
        </div>
        <br />
        文件大小不能超过<span style="color: #65b1ff">30MB</span>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirmImportFace">确 定</el-button>
        <el-button>取 消</el-button>
      </div>

 2.方法合集

    // 导入附件data数据
      uploadFace: {
        //人员ID
        employeeId: null,
        // 是否显示弹出层(导入)
        open: false,
        // 弹出层标题(导入)
        title: "附件上传",
        //上传列表
        fileList: [],
        //上传文件
        files: null,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url:
          process.env.VUE_APP_BASE_API +
          "url接口地址",
      },    

   



    /* 打开选取 */
    handleFace(row) {
      this.uploadFace.open = true;
      this.uploadFace.employeeId = row.id;
      this.uploadData = {
        id: this.uploadFace.employeeId,
      };
    },
    /* 列表删除时 */
    handleRemove(file, fileList) {
      this.uploadFace.files = null;
      this.uploadFace.fileList = fileList;
    },

    /* 选取成功时 */
    handleAvatarSuccess(response, file, fileList) {
      this.uploadFace.files = file.raw;
      console.log(this.uploadFace.fileList);
    },

    // 处理文件上传操作
    handleFileUpload(file) {
      const formData = new FormData();
      // 向 formData 对象中添加文件
      formData.append("id", this.uploadFace.employeeId);
      formData.append("file", file.file);
      faceInput(formData).then((result) => {
        if (result.code != 200) {
          return;
        }
        this.$message({
          message: "上传成功",
          type: "success",
        });
        this.handleRemove()
        this.uploadFace.open = false;
      });
    },
    /* 确定上传 */
    confirmImportFace() {
      this.$refs.upload.submit();
    },

 3.接口封装

// 文件上传
export function faceInput(formData) {
  return request({
    url: "url",
    method: "post",
    data: formData,
    headers: {
      "Content-Type": "multipart/form-data;boundary =" + new Date().getTime(),
    },
  });
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
您好,关于 element-ui上传组件 el-upload,您可以通过自定义 el-upload-list 来实现自定义上传列表的功能。 具体实现方法如下: 1. 在 el-upload 组件中添加属性 list-type="picture-card",并设置属性 action 为上传文件的地址。 2. 在 el-upload 组件中添加自定义 slot,用于自定义上传列表的显示。 3. 在自定义上传列表组件中,使用 el-upload-list 组件来展示上传文件的列表。 4. 可以通过监听 el-upload 组件的 change 事件来获取上传的文件信息,并将上传的文件信息传递给自定义上传列表组件,以展示上传文件的列表。 以下是一个简单的示例代码: ```html <template> <div> <el-upload class="upload-demo" action="/upload" list-type="picture-card" :on-change="handleChange" > <i class="el-icon-plus"></i> <div class="upload-text">上传图片</div> </el-upload> <custom-upload-list :file-list="fileList"></custom-upload-list> </div> </template> <script> import CustomUploadList from './CustomUploadList.vue'; export default { components: { CustomUploadList, }, data() { return { fileList: [], }; }, methods: { handleChange(file, fileList) { this.fileList = fileList; }, }, }; </script> ``` CustomUploadList.vue 组件的代码如下: ```html <template> <el-upload-list class="custom-upload-list" :file-list="fileList"> <template slot-scope="{file}"> <div class="custom-list-item"> <img :src="file.url" class="custom-list-item-thumbnail"> <div class="custom-list-item-name">{{ file.name }}</div> <div class="custom-list-item-actions"> <el-button size="small" type="text" @click="handleRemove(file)">删除</el-button> </div> </div> </template> </el-upload-list> </template> <script> export default { props: { fileList: { type: Array, default: () => [], }, }, methods: { handleRemove(file) { const index = this.fileList.indexOf(file); if (index !== -1) { this.fileList.splice(index, 1); } }, }, }; </script> ``` 希望这个示例能够帮助到您。如果您有任何问题,请随时提出。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值