Vue3+ant3.0上传文件blob变成[object, object]

本文探讨了在Vue3中使用Ant Design Upload组件时,由于数据劫持导致file变量类型转换的问题。通过实例代码,作者揭示了如何避免HTML中的响应式绑定,并提供了将v-model改为:before-upload的方法来修复这个问题。

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

<template>
  <div class="clearfix">
    <a-upload :before-upload="beforeUpload" v-model:file-list="fileList">
      <a-button :disabled="!!fileList.length" >
        <upload-outlined></upload-outlined>
        上传文件
      </a-button>
    </a-upload>
  </div>
</template>
<script lang="ts" setup>
import { UploadOutlined } from '@ant-design/icons-vue';
import { UploadFileTool } from "@/api";
import type { UploadProps } from 'ant-design-vue';
import { ref } from 'vue';
const fileList = ref<UploadProps['fileList']>([]); //文件列表
const beforeUpload = (file: any) => {
    fileList.value = [...fileList.value, file];
    return false;
};
 const handleUpload = () => {
      const formData = new FormData();
      fileList.value.forEach((file: UploadProps['fileList'][number]) => {
        formData.append('files[]', file as any);
      });
      console.log(formData.get('files[]'))
    };
defineExpose({
    handleUpload
})
</script>


  • 以上是问题代码 file变量本来是blob会变成[object, object]

问题分析原因,vue3的数据劫持功能影响。

  • 解决办法 将 v-model:file-list=“fileList” 变成**:before-upload=“beforeUpload”**不要HTML中的响应
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值