: 파일은 아무 형태로나 서버로 전달할 수 없다. 위에서 받아온 file도 그 형태 그대로 전달 할 수 없다.
때문에 formData 라는 것을 사용하게 된다.
let formData = new FormData();
: 가장 많이 쓰게 될 formdata의 내장 함수로 form 데이터 안에 키,값의 형태로 담을때 사용된다.
const upload = () => {
let formData = new FormData();
formData.append('files', files);
};
: file과 text 동시에 보내기
const upload = () => {
const temp = JSON.stringify({
name: selectedClassName,
price: selectedPrice,
sale: selectedDiscount / 100,
});
let formData = new FormData();
formData.append('body', temp);
formData.append('files', files);
};
: file은 리스트(배열)의 형태로 만들어 formData에 바로 담을 수 없다.
(이 경우 빈 객체만 나오는 것을 알 수 있다)
const upload = () => {
let formData = new FormData();
for (let i = 0; i < files.length; i++) {
formData.append('files', files[i]);
}
};