ํด๋ผ์ด์ธํธ
const fileUpload = async (file: Blob) => {
const formData = new FormData();
formData.append('file', file, file.name); // ํ์ผ ์ด๋ฆ์ ๋ช
์
try {
const res = await fetch('/api/upload', {
method: 'POST',
body: formData,
});
const data = await res.json();
console.log('์๋ฒ ์๋ต:', data);
} catch (error) {
console.error('ํ์ผ ์
๋ก๋ ์ค ์ค๋ฅ:', error);
}
};
export default fileUpload;
์๋ฒ(api/upload/route.ts.
import { NextRequest, NextResponse } from 'next/server';
import { writeFile } from 'fs/promises';
import { join } from 'path';
export const config = {
api: {
bodyParser: false,
},
};
export default async function handler(request: NextRequest, response: NextResponse) {
console.log('์
๋ก๋ API');
const data = await request.formData();
const file: File | null = data.get('file') as unknown as File;
if (!file) NextResponse.json({ success: false });
const bytes = await file.arrayBuffer();
const buffer = Buffer.from(bytes);
const path = join('/', 'tmp', file.name);
await writeFile(path, buffer);
console.log(`${path}}`);
NextResponse.json('์
๋ก๋ API');
}
'๐ํ๋ก๊ทธ๋๋ฐ > Next.js' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Next.js] ๋ฆฌ์ํธ ํ ์ด๋ธ CSV ์ ์ฅ ๊ตฌํ(feat. Shadcn) (0) | 2024.06.21 |
---|---|
[์ ๋ณด] NextAuth.js Type ์๋ฌ ์ก๊ธฐ - CredentialsProvider / prisma (0) | 2024.02.07 |
[Nextjs] api return ๊ฐ ๊ฐฑ์ ์๋๋ ์ด์ (0) | 2023.12.04 |
[nextjs] ์์ ํ์ผ ๋ค์ด๋ก๋ (1) | 2023.11.27 |
[์ ๋ณด] next.js localhost:3000 ์ ์ ๋ถ๊ฐ (0) | 2023.07.22 |