Skip to content

Commit

Permalink
fix: 이미지 업로드 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
양회진 authored and 양회진 committed Sep 25, 2023
1 parent ef58b2b commit 6cadb2f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/pages/Gathering/Host.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useEffect, useState } from 'react';
import Style from './HostStyle.jsx';
import Style from './HostStyle';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faChevronDown, faStar } from '@fortawesome/free-solid-svg-icons';
import { styled } from 'styled-components';
Expand Down
25 changes: 10 additions & 15 deletions src/pages/Registration/Registration.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ const Registration = () => {
const [visibleTime, setVisibleTime] = useState('00:00');
const [afterPost, setAfterPost] = useState(false);
const [saveRoomId, setSaveRoomId] = useState();
const [saveFormdata, setSaveFormdata] = useState({});

const { getData: ageDatas } = useFetch(
//백엔드용
Expand Down Expand Up @@ -97,30 +96,26 @@ const Registration = () => {
setVisibleTime(value);
};

let formData = new FormData();

const saveImgFile = _formdata => {
const saveImgFile = () => {
const file = imgRef.current.files[0];
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onloadend = () => {
setUploadImg(reader.result);
};
_formdata.append('image', file);
_formdata.append('roomId', saveRoomId);
setSaveFormdata(formData);
// for (let value of formData.values()) {
// console.log(value);
// }
return _formdata;
};
const postImg = async _formdata => {

const postImg = async () => {
const imgData = new FormData();
imgData.append('image', imgRef.current.files[0]);
imgData.append('roomId', saveRoomId);

await fetch(`http://${process.env.REACT_APP_IP}/rooms/image`, {
method: 'POST',
headers: {
Authorization: `Bearer ${token}`,
},
body: _formdata,
body: imgData,
})
.then(response => {
return response.json();
Expand Down Expand Up @@ -364,7 +359,7 @@ const Registration = () => {
type="file"
accept="image/*"
id="file"
onChange={() => saveImgFile(formData)}
onChange={saveImgFile}
ref={imgRef}
/>
</style.FileForm>
Expand All @@ -376,7 +371,7 @@ const Registration = () => {
<style.RegisteBtn
disabled={!imgRef?.current?.files[0]?.name}
condition={imgRef?.current?.files[0]?.name}
onClick={() => postImg(saveFormdata)}
onClick={postImg}
>
등록
</style.RegisteBtn>
Expand Down

0 comments on commit 6cadb2f

Please sign in to comment.