728x90
๋ฐ์ํ
Only plain objects, and a few built-ins, can be passed to Server Actions. Classes or null prototypes are not supported.
๐ก์ํ ์ ์ฅ ์ ์๋ฒ์ ์ด๋ฏธ์ง๋ฅผ ๋จผ์ ์ ๋ก๋ ํ๋ ค ํ์ผ๋
์ผ๋ฐ ๊ฐ์ฒด๊ฐ ์๋ ์ง๋ ฌํ ํ ์ ์๋ ํด๋์ค๋ null ํ๋กํ ํ์ ์ ๊ฐ์ง ๊ฐ์ฒด๋ ์๋ฒ ์ปดํฌ๋ํธ๋ก ๋๊ธฐ์ง ๋ชปํ๋ค๋ ์๋ฌ๋ฅผ ๋ฐ์๋ค.
ํด๊ฒฐ์ฑ FormData
ํด๋ผ์ด์ธํธ > ์๋ฒ ์ ์ก ์ ๋ฐ์ดํฐ๋ฅผ ์ง๋ ฌํํด ์๋ฒ์ ์ ๋ฌํ๊ณ , ์๋ฒ๋ ์ด๋ฅผ ์ญ์ง๋ ฌํํด ์ฒ๋ฆฌํ๋๋ฐ FormData๋ ํ์ผ๊ณผ ๊ฐ์ ๋น๊ตฌ์กฐ์ ๋ฐ์ดํฐ๋ฅผ ์ง์ ๋ฐ์ด๋๋ฆฌ ํํ๋ก ์ ์ก๋๋ฏ๋ก ๋ณ๋์ ์ง๋ ฌํ ๊ณผ์ ์ด ํ์ํ์ง ์์ต๋๋ค.
์์ ์
const handleFileChange = async (e: ChangeEvent<HTMLInputElement>) => {
const files = e.target.files;
if (!files) return;
for (let i = 0; i < files.length; i++) {
//file ํ์
์ง์ ๋๊ธฐ๊ธฐ
const res = await imageUpload(files[i], 'temp');
}
};
์์ ํ
const handleFileChange = async (e: ChangeEvent<HTMLInputElement>) => {
const files = e.target.files;
if (!files) return;
for (let i = 0; i < files.length; i++) {
const formData = new FormData();
formData.append('file', files[i]);
// ์์ ๋ formData ๋๊ธฐ๊ธฐ
const res = await imageUpload(formData, 'temp');
}
};
ํด๊ฒฐ์ฑ API Route
์๋ฒ ์ก์ ์ ํด๋ผ์ด์ธํธ์์ ์ง๋ ฌํํ ์ ์๋ ๊ฐ์ฒด(File, Blob, Buffer ๋ฑ)๋ฅผ ๋ฐ์ ์ ์๊ธฐ์ API ๋ผ์ฐํธ๋ ์ธ๋ถ API ์ฌ์ฉํ ์ ์์ต๋๋ค.
const response = await fetch('/api/getImageUrl');
const { url } = await response.json();
// S3์ ํ์ผ ์
๋ก๋
await fetch(url, {
method: 'PUT',
body: file,
headers: {
'Content-Type': file.type,
},
});
ํ์๋ API Route ์์ด Server Action๊ณผ Prisma๋ฅผ ์ฌ์ฉํ๊ธฐ์ FormData๋ฅผ ์ฌ์ฉํ์์ต๋๋ค.
๊ฐ์ฌํฉ๋๋ค.
728x90
๋ฐ์ํ
'NEXT' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
AWS S3 Access Denied CopyObjectCommand (0) | 2024.10.18 |
---|---|
NEXT JS + Google Analytics Connect Setting (0) | 2024.08.07 |
Parallel Routes Modal Difficult Error (0) | 2024.07.25 |
NextJs Next-auth getSession() ๋ก๊ทธ์ธ ์ ๋ณด ํ์ธ (0) | 2024.07.23 |
NEXT 14 NEXT-AUTH 5 middleware.ts not working (0) | 2024.07.11 |