kozy
Next 13 FormData image upload tool via API route
npm i kozy
yarn add kozy
pnpm add kozy
It is very easy to use. After installing the package, create a route called upload under the api folder and add the following code.
export async function POST(req: NextRequest, res: NextResponse) {
const form: any = await req.formData();
const file = form.get('avatar');
const uploaded: any = await UploadImage({
file,
folder: 'uploads',
name: `hello_world`,
size: '2 MB',
});
return NextResponse.json({ data: uploaded.file, isSuccess: true });
}