π Form Data νμμΌλ‘ 첨λΆνμΌ μ μ‘νκ³ λ°μμ€κΈ°
β«οΈ newsAjax.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://code.jquery.com/jquery-latest.min.js"></script>
<style>
...
</style>
</head>
<body>
<div id="container">
<form enctype="multipart/form-data">
<table>
<thead>
<tr>
<th colspan="2">
<h1>λ΄μ€μ 보</h1>
</th>
</tr>
</thead>
<tbody>
<tr>
<th>
<label for="title">μ λͺ©</label>
</th>
<td>
<input type="text" id="title" name="title">
</td>
</tr>
<tr>
<th>
<label for="broadcasting-name">λ°©μ‘κ΅λͺ
</label>
</th>
<td>
<input type="text" id="broadcasting-name" name="broadcastingName">
</td>
</tr>
<tr>
<th>
<label for="broadcasting-name">첨λΆμ΄λ―Έμ§</label>
</th>
<td>
<!-- νμΌ, μ΄λ―Έμ§ -->
<input type="file" name="files" accept="image/jpeg, image/png, image/jpg, image/gif" multiple>
</td>
</tr>
<tr>
<th>
<label for="content">κΈ°μ¬λ΄μ©</label>
</th>
<td>
<textarea id="content" name="content"></textarea>
</td>
</tr>
</tbody>
<tfoot>
<tr>
<th colspan="2">
<button type="button" class="write-button">μμ±νκΈ°</button>
</th>
</tr>
</tfoot>
</table>
</form>
<div class="preview">
</div>
</div>
<script>
// 'μμ±νκΈ°' λ²νΌ ν΄λ¦ μ Ajax λ‘
// μλ²μ νΌ λ°μ΄ν° νμμΌλ‘ κΈ°μ¬μ 첨λΆνμΌμ ν¨κ» μ μ‘(request)ν¨.
const writeButton = document.querySelector(".write-button");
writeButton.onclick = () => {
request();
}
function getFormData() {
const form = document.querySelector("form");
return new FormData(form);
}
function request() {
$.ajax({
async: false,
type: "post",
url: "/api/news",
// form-data μ μ‘ μ μλ 3 μ€μ μ ν΄μ€μΌ μ λλ‘ μ μ‘λ¨ β
enctype: "multipart/form-data",
contentType: false,
processData: false,
data: getFormData(),
dataType: "json",
success: (response) => {
console.log(response);
const preview = document.querySelector(".preview");
preview.innerHTML = `
<h2>μ λͺ©: ${response.data.title}</h2>
<h2>λ°©μκ΅λͺ
: ${response.data.broadcastingName}</h2>
<h2>λ΄μ©: ${response.data.content}</h2>
<h2>νμΌ λ¦¬μ€νΈ</h2>
`;
for(let i = 0; i < response.data.fileNames.length; i++) {
preview.innerHTML += `
<h3>${response.data.fileNames[i]}</h3>
`;
}
},
error: (error) => {
console.log(error);
}
})
}
</script>
</body>
</html>
<input> νκ·Έμ name μΌλ‘ μλ²μμ νΌ λ°μ΄ν°λ₯Ό μ°Έμ‘°ν λ μ¬μ©ν μ΄λ¦μ μ§μ ν΄μ€λ€.
DTOμ νλλͺ
κ³Ό λμΌν΄μΌ DTO κ°μ²΄μ μμ² λ°μ΄ν°λ₯Ό λ΄μμ¬ μ μλ€.
β«οΈ AddNewsReqDto.java
@Data
public class AddNewsReqDto {
private String title;
private String broadcastingName;
private List<MultipartFile> files;
private String content;
}
β«οΈ NewsController
@Slf4j
@RestController
public class NewsController {
@PostMapping("/api/news")
public ResponseEntity<?> addNews(AddNewReqDto addNewReqDto){
log.info("{}", addNewReqDto);
Map<String, Object> map = new HashMap<String, Object>();
map.put("title", addNewReqDto.getTitle());
map.put("broadcastingName", addNewReqDto.getBroadcastingName());
map.put("content", addNewReqDto.getContent());
List<String> fileNameList = new ArrayList<String>();
addNewReqDto.getFiles().forEach((file) -> {
fileNameList.add(file.getOriginalFilename());
});
map.put("fileNames", fileNameList);
return ResponseEntity.ok(new CMRespDto<>(1, "λ΄μ€ λ±λ‘ μλ£", map));
}
}
@RequestBody μ΄λ
Έν
μ΄μ
μ μν΄
map μΌλ‘ μλ΅ λ°μ΄ν°λ₯Ό 보λ΄λ©΄ β JSON κ°μ²΄λ‘ 보λ΄μ§.
π’ μκ° π