<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中的响应