diff --git a/frontend/src/post/posting.tsx b/frontend/src/post/posting.tsx index 8b359cefc311bf768b22321cfd11aef7f16791c1..62c7648459adfa8e1a83928a0825fdba5f6b10c4 100644 --- a/frontend/src/post/posting.tsx +++ b/frontend/src/post/posting.tsx @@ -5,12 +5,15 @@ import equals from "validator/lib/equals"; import { catchErrors } from "../helpers"; import { PostType } from "../types"; import { postApi } from "../apis"; +import { File } from "formidable"; export default function Posting() { const [city, setCity] = useState("질문종류"); const [theme, setTheme] = useState("질문종류"); const [title, setTitle] = useState(""); const [text, setText] = useState(""); + const [file, setFile] = useState(); + const [imgSrc, setImgSrc] = useState(); const navigate = useNavigate(); const [user, setUser] = useState({ @@ -24,6 +27,21 @@ export default function Posting() { _id: "", }); + const imgArr = new Array(); + + const img2Url = (fileBlob: Blob) => { + const reader = new FileReader(); + reader.readAsDataURL(fileBlob); + + reader.onload = (data) => { + if (typeof data.target?.result === "string") { + console.log(data.target?.result); + imgArr.push(data.target?.result); + // setImgSrc(data.target?.result); + } + }; + }; + const [loading, setLoading] = useState(false); const [error, setError] = useState(""); const [disabled, setDisabled] = useState(false); @@ -102,16 +120,37 @@ export default function Posting() { setUser(newUser); }; + const handleInputPic = (event: React.ChangeEvent) => { + // const fileArr = new FileList; + const maxImg = 5; + const fileArr = event.target.files; + if (!(fileArr?.length === undefined)) { + if (!(fileArr?.length > maxImg)) { + for (var i = 0; i < maxImg; i++) { + // setFile(file); + console.log(fileArr[i]); + img2Url(fileArr[i]); + } + setImgSrc(imgArr); + } else { + alert("사진은 최대 5장까지 업로드 가능합니다!"); + } + } + }; + return ( -
+

Id

-

empty

+

empty

+
+ +
@@ -151,26 +190,34 @@ export default function Posting() {
-
+
-
+
+
+ {/* {imgSrc?.map((img, i) => ( +
+ +
+ ))} */} + {/* */} +