Version: 4.xx.xx
Base64 Upload
By encoding your files and images from your forms to Base64 you can change all files needed for the upload to Base64 format before the submit. This can be done via the onFinish
property of the <Form>
component that comes with Ant Design
Example
Now let's make a small example to see how its done. In this example, the file we are going to be uploading files in Base64 type is going to be called avatar
pages/users/create.tsx
import {
file2Base64,
} from "@refinedev/core";
import {
Create,
useForm,
getValueFromEvent,
} from "@refinedev/antd";
import { Form, Upload, Input } from "antd";
export const UserCreate: React.FC = () => {
const { form, formProps, saveButtonProps } = useForm<IUser>();
return (
<Create saveButtonProps={saveButtonProps}>
<Form
{...formProps}
layout="vertical"
onFinish={async (values) => {
const base64Files = [];
// @ts-ignore
const { avatar } = values;
for (const file of avatar) {
if (file.originFileObj) {
const base64String = await file2Base64(file);
base64Files.push({
...file,
base64String,
});
} else {
base64Files.push(file);
}
}
return (
formProps.onFinish &&
formProps.onFinish({
...values,
avatar: base64Files,
})
);
}}
>
<Form.Item
label="First Name"
name="firstName"
rules={[
{
required: true,
},
]}
>
<Input />
</Form.Item>
<Form.Item label="Avatar">
<Form.Item
name="avatar"
valuePropName="fileList"
getValueFromEvent={getValueFromEvent}
noStyle
rules={[
{
required: true,
},
]}
>
<Upload.Dragger
listType="picture"
multiple
beforeUpload={() => false}
>
<p className="ant-upload-text">Drag & drop a file in this area</p>
</Upload.Dragger>
</Form.Item>
</Form.Item>
</Form>
</Create>
);
};
interface IUser {
id: number;
firstName: string;
avatar: [
{
uid: string;
name: string;
url: string;
status: "error" | "success" | "done" | "uploading" | "removed";
},
];
}
You can change files to Base64 by using the file2Base64
function.
TIP
An edit form can be made by using the <Edit>
component instead of <Create>
without changing the rest of the code.
Example
Run on your local
npm create refine-app@latest -- --example upload-antd-base64