Newer
Older
import React, { useRef, useState, useEffect, useContext } from 'react';
import { useNavigate } from 'react-router-dom';
import { UserContext } from '../Usercontext.js';
import SearchMap from '../components/SearchMapByKeyword.js'
const [location, setLocation] = useState({
keyword: "",
center: { lat: null, lng: null }
});
const navigate = useNavigate();
.then((response) => {
if (!response.data) {
alert("세션이 만료되었습니다. 다시 로그인 바랍니다.")
MoveTo('/login')
}
})
.catch((response)=>{
console.log("error!:LogOut")
console.log(response)
})
});
const [inputs, setInputs] = useState({
title: "",
content: "",
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
const [previewImg, setPreviewImg] = useState({
imageUrls: [],
});
const imgRef = useRef();
// 이미지 업로드 input의 onChange
const saveImgFile = async () => {
console.log(imgRef.current.files)
const file = imgRef.current.files[0];
console.log(file)
inputs.imageUrls.push(file);
setInputs({
...inputs,
imageUrls: inputs.imageUrls
})
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onloadend = () => {
previewImg.imageUrls.push(reader.result);
setPreviewImg({
imageUrls: previewImg.imageUrls
});
};
};
let imglist = [];
imglist = previewImg.imageUrls.map((image)=>{
return(
<img
src={image ? image :null}
alt="이미지"
style={{ width: "100px", height: "100px"}}
/>
)
}
);
const handleSubmit= async (event) => {
event.preventDefault();
url: 'http://localhost:8080/post/upload', // 통신할 웹문서
headers: {'Content-Type': 'multipart/form-data', charset: 'utf-8'},
return response.data;
}
else {
return null;
}
}
catch (error){
console.log('Error during post creation:', error);
}
}
const onChange = (e) => {
const {value, name} = e.target;
setInputs({
...inputs,
[name]: value
});
const [inputs, setInputs] = useState({
title: "",
content: "",
});
const [content, setContent] = useState("");
const [uploadedImg, setUploadedImg] = useState({
fileName: "",
fillPath: ""
});
const BASE_URL = "http://localhost:3000";
const onTextChange = (e) => {
const {value, name} = e.target;
setInputs({
...inputs,
[name]: value
});
}
const onImgChange = e => {
let temp = []
for (let i=0; i< e.target.files.length; i++) {
temp.push(e.target.files[i])
}
setContent(e.target.files);
const onSubmit = e => {
e.preventDefault();
const formData = new FormData();
Array.from(content).forEach((el) => {
formData.append("img", el);
});
formData.append("title", inputs.title);
formData.append("content", inputs.content);
.post("http://localhost:8080/post/upload", formData,
{
headers: {"Content-Type": "multipart/form-data"}
})
.then(res => {
const { fileName } = res.data;
console.log(fileName);
setUploadedImg({ fileName, filePath: `${BASE_URL}/img/${fileName}` });
alert("The file is successfully uploaded");
})
.catch(err => {
console.error(err);
});
};
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<section>
<div style={{float: "left"}}>
<SearchMap loc={location} setLoc={setLocation}></SearchMap>
</div>
<div style={{float: "right"}}>
<form onSubmit={onSubmit}>
<div>
<label>제목</label>
<input
type="text"
name='title'
onChange={onTextChange}
required/>
</div>
<div>
<label>Content</label>
<input
type="text"
name='content'
onChange={onTextChange}
required/>
</div>
<input type="file" onChange={onImgChange} multiple/>
<button type="submit">Upload</button>
</form>
</div>
</section>
</>
)
/*
return(
<div>
<aside style={{float: "left"}}>
<SearchMap loc={location} setLoc={setLocation}></SearchMap>
</aside>
<main style={{float: "right"}}>
<form onSubmit={handleSubmit} enctype="multipart/form-data">
<div>
<label>제목</label>
<input
type="text"
name='title'
//value={title}
onChange={onChange}
required
/>
</div>
<div>
<label>Content</label>
<textarea
name='content'
//value={content}
onChange={onChange}
/>
</div>
<button style={{height:"50px"}}>wow</button>
<div>
{imglist}
<label className="signup-profileImg-label" htmlFor="profileImg">이미지 추가</label>
<input
name="image"
style={{ display: "none"}}
className="signup-profileImg-input"
type="file"
accept="image/*"
id="profileImg"
onChange={()=>{saveImgFile()}}
ref={imgRef}/>
</div>