리액트 커스텀훅 이미지 파일을 axios post

안녕하세요·2024년 2월 16일

react

목록 보기
12/32
import axios from "axios";

export const useImageUpload = () => {
    const imageUpload = async (file) => {
        const formData = new FormData();
        formData.append("file", file);

        try {
            const result = await axios.post('http://localhost:8000/imgupload', formData);
            return result.data; 
        } catch (error) {
            console.error("이미지 업로드 중 오류 발생:", error);
            return ''; 
        }
    };

    return { imageUpload };
};

useImageUpload.js

    const { imageUpload } = useImageUpload();
    let imgPath = await imageUpload(e.target.files[0]);

활용

0개의 댓글