EDIT 기능 추가

This commit is contained in:
daniel LEE 2023-11-09 14:51:43 +09:00
parent 7078a17073
commit eabb7968b3
2 changed files with 108 additions and 17 deletions

View File

@ -123,9 +123,69 @@ public class UserProfileController {
} }
@PutMapping("/user/{id}") @PutMapping("/user/{id}")
public void putUserProfile(@PathVariable String id, @RequestParam String name, public void putUserProfile(@PathVariable("id") String id, @RequestParam("name") String name,
@RequestParam String phone, @RequestParam String address, @RequestParam String filepath) { @RequestParam("phone") String phone, @RequestParam("address") String address,
mapper.updateUserProfile(id, name, phone, address, filepath); @RequestPart MultipartFile upfile) {
String uuid = UUID.randomUUID().toString();
String fileName = upfile.getOriginalFilename();
String fileExtName = fileName.substring(fileName.lastIndexOf("."), fileName.length());
String contentType = upfile.getContentType();
long fileLength = upfile.getSize();
System.out.println("fileLength : " + fileLength);
System.out.println("fileName : " + fileName);
System.out.println("fileExtName : " + fileExtName);
System.out.println("contentType : " + contentType);
byte[] data = null;
try {
data = upfile.getBytes();
} catch (IOException e) {
e.printStackTrace();
}
String baseFolder = SERVER_URL_IMG;
String baseMiddleFolder = "/img";
String saveFolder = baseFolder + baseMiddleFolder;
String dbSavePath = baseMiddleFolder + "/" + uuid + fileExtName;
String saveFullFilePath = saveFolder + "/" + uuid + fileExtName;
// 입력된 내용 파일로 쓰기
File folder = new File(saveFolder);
// 해당 디렉토리가 없을경우 디렉토리를 생성합니다.
if (!folder.exists()) {
try {
folder.mkdir(); // 폴더 생성합니다.
System.out.println(saveFolder + " folder create OK");
} catch (Exception e) {
e.getStackTrace();
}
} else {
System.out.println("A folder has already been created");
}
File file = new File(saveFullFilePath);
System.out.println("saved file : " + saveFullFilePath);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
fos.write(data);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fos != null)
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
mapper.updateUserProfile(id, name, phone, address, dbSavePath);
} }
@DeleteMapping("/user/{id}") @DeleteMapping("/user/{id}")

View File

@ -17,24 +17,19 @@
formData.append("name", $('#name').val()); formData.append("name", $('#name').val());
formData.append("phone", $('#phone').val()); formData.append("phone", $('#phone').val());
formData.append("address", $('#address').val()); formData.append("address", $('#address').val());
formData.append("upfile", $("input[name='upfile']")[0].files[0]); formData.append("upfile", $("input[name='upfile']")[0].files[0]);
/* formData.append("upfile", $('#upfile').files[0]); */
for (var pair of formData.entries()) { for (var pair of formData.entries()) {
console.log(pair[0]+ ', ' + pair[1]); console.log(pair[0]+ ', ' + pair[1]);
} }
$.ajax({ $.ajax({
type: "POST", type: "POST",
url: '/profile/user/10', url: '/profile/user/10',
/* data: $('#theForm').serialize(), */
data: formData, data: formData,
contentType: false, contentType: false,
processData: false, processData: false,
success: function(data){ success: function(data){
//location.href="listUser.do";
alert("신규등록 OK"); alert("신규등록 OK");
}, },
error: function(error, status, msg) { error: function(error, status, msg) {
@ -42,6 +37,35 @@
} }
}); });
}); });
//
$('#edit').on('click', function() {
var formData = new FormData();
formData.append("name", $('#ename').val());
formData.append("phone", $('#ephone').val());
formData.append("address", $('#eaddress').val());
formData.append("upfile", $("input[name='eupfile']")[0].files[0]);
for (var pair of formData.entries()) {
console.log(pair[0]+ ', ' + pair[1]);
}
$.ajax({
type: "PUT",
url: '/profile/user/1',
data: formData,
contentType: false,
processData: false,
success: function(data){
alert("수정등록 OK");
},
error: function(error, status, msg) {
alert("Error " + msg);
}
});
});
}); });
</script> </script>
@ -68,11 +92,18 @@
</form> --> </form> -->
제품명 <input type="text" id="name" name="name" value=""/><br> 제품명 <input type="text" id="name" name="name" value=""/><br>
가격 <input type="text" id="phone" name="phone" value=""/><br> 가격 <input type="text" id="phone" name="phone" value=""/><br>
별 <input type="text" id="address" name="address" value=""/><br> 별 <input type="text" id="address" name="address" value=""/><br>
이미지 <input type="file" id="upfile" name="upfile" > 이미지 <input type="file" id="upfile" name="upfile">
<button id="add" type="button" class="btn btn-primary">Submit</button> <button id="add" type="button">Submit</button>
<br><br><br><br><br><br>
제품명 <input type="text" id="ename" name="ename" value=""/><br>
가격 <input type="text" id="ephone" name="ephone" value=""/><br>
별 <input type="text" id="eaddress" name="eaddress" value=""/><br>
이미지 <input type="file" id="eupfile" name="eupfile">
<button id="edit" type="button">Submit</button>
</body> </body>
<iframe name='blankifr' style='display:none;'></iframe>
</html> </html>