1.customRequest 自定义上传
customRequest={uploadHandler}
const uploadHandler = async (options: any) => {
const { onSuccess, file } = options;
if (!file) {
return false;
}
const fd = new FormData();
fd.append("file", file);
const res = await fetch(uploadUrl, {
method: "POST",
credentials: "include",
body: fd,
});
if (!res.ok) {
const err = await res.json();
return false;
}
const { url } = await res.json();
console.log('image url result: ', url, file);
file.url = url
onSuccess(res, file)
return true;
};
2.在Form中使用
Upload组件在Form中使用,会处于受控状态,所以默认值如FileList的初始化需要通过Form的setFieldsValue方法来设置
const [form] = Form.useForm();
form.setFieldsValue({
banner: [{ uid: uuid().toString(), name: imageURL, status: 'done', url: imageURL}],
})
<Form.Item