728x90
html
js
file์ JSON์ ํฌํจ๋ ์ ์๋ค. ๊ทธ๋์ FormData ์์ file๊ณผ JSON (= data)๋ฅผ append ์ํจ๋ค.
//FormData ์๋ก์ด ๊ฐ์ฒด ์์ฑ
var formData = new FormData();
// ๋๊ธธ ๋ฐ์ดํฐ๋ฅผ ๋ด์์ค๋ค.
var data = {
"userId" : $("#userId").val(),
"title" : $("#title").val(),
"contents" : $("#contents").val()
}
// input class ๊ฐ
var fileInput = $('.files');
// fileInput ๊ฐ์๋ฅผ ๊ตฌํ๋ค.
for (var i = 0; i < fileInput.length; i++) {
if (fileInput[i].files.length > 0) {
for (var j = 0; j < fileInput[i].files.length; j++) {
console.log(" fileInput[i].files[j] :::"+ fileInput[i].files[j]);
// formData์ 'file'์ด๋ผ๋ ํค๊ฐ์ผ๋ก fileInput ๊ฐ์ append ์ํจ๋ค.
formData.append('file', $('.files')[i].files[j]);
}
}
}
// 'key'๋ผ๋ ์ด๋ฆ์ผ๋ก ์์์ ๋ด์ data๋ฅผ formData์ appendํ๋ค. type์ json
formData.append('key', new Blob([ JSON.stringify(data) ], {type : "application/json"}));
// ajax ์ฒ๋ฆฌ ๋ถ๋ถ *
$.ajax({
url: 'url',
data: formData,
contentType: false, // * ์ค์ *
processData: false, // * ์ค์ *
enctype : 'multipart/form-data', // * ์ค์ *
success: function(data) {
if (result.SUCCESS == true) {
alert("์ฑ๊ณต");
} else {
alert("์คํจ");
}
}
});
- contentType : false ๋ก ์ ์ธ ์ content-type ํค๋๊ฐ multipart/form-data๋ก ์ ์ก๋๊ฒ ํจ
- processData : false๋ก ์ ์ธ ์ formData๋ฅผ string์ผ๋ก ๋ณํํ์ง ์์
service
public void insertMypageQna(Map<String, Object> param, List<MultipartFile> fileList) throws Exception {
boardMapper.insertMypageQna(param);
List<Map<String,Object>> list = fileutils.fileUpload(param, fileList);
if(list!=null){
for(int i = 0; i < list.size(); i++){
boardMapper.insertFile(list.get(i));
}
}
}
xml
<insert id="insertFile" parameterType="map">
INSERT INTO COMMONFILES(
file_no,
board_no,
original_file_name,
stored_file_name,
file_size,
create_date,
create_user,
del_y_n
)
VALUES(
NEXTVAL('file_no'),
#{boardNo},
#{originalFileName},
#{storedFileName},
#{fileSize},
current_timestamp,
#{createUser},
'N'
)
</insert>
controller
@RequestMapping(value="/insertMypageQna.do", method=RequestMethod.POST)
@ResponseBody
public Map<String, Object> insertMypageQna(
@RequestPart(value = "key") Map<String, Object> param,
@RequestPart(value = "file",required = false) List<MultipartFile> fileList) throws Exception {
Map<String, Object> result = new HashMap<String, Object>();
bservice.insertBoard(param,fileList);
result.put("SUCCESS", true);
// result๋ฅผ return ํ๋ฉด ajax success์์ result.SUCCESS == true๋ก if๋ฌธ ์ฌ์ฉ๊ฐ๋ฅ
return result;
}
globals. properties
# ํ์ผ ์
๋ก๋ ๊ฒฝ๋ก(๊ฒฝ๋ก ์ค์ ์ ๋ฐ๋์ ์ ๋๊ฒฝ๋ก๋ฅผ ์ฌ์ฉํด์ผํจ, ๊ฒฝ๋ก ๋ค์ /๋ฅผ ๋ถ์ฌ ์ฃผ์ด์ผํจ.)
Globals.filePath = C:/file/upload/
FileUtils.java
//ํ์ผ ์
๋ก๋
public static List<Map<String,Object>> fileUpload(Map<String,Object> map, List<MultipartFile> fileList) throws Exception{
String user = (String) map.get("memWriter");
int board_no = (int)map.get("board_no");
List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
Map<String, Object> listMap = null;
for(MultipartFile mf : fileList){
listMap = new HashMap<String,Object>();
listMap.put("boardNo", board_no);
listMap.put("createUser", user);
listMap.put("originalFileName", mf.getOriginalFilename());
// ์ ์ฅ์ด๋ฆ
listMap.put("storedFileName", CommonUtils.getRandomString() + mf.getOriginalFilename());
listMap.put("fileSize", mf.getSize());
list.add(listMap);
// globals.properties
File dest = new File(EgovProperties.getProperty("Globals.filePath") + listMap.get("storedFileName"));
if(!dest.exists()){
dest.mkdirs(); //๋๋ ํ ๋ฆฌ๊ฐ ์กด์ฌํ์ง ์๋๋ค๋ฉด ์์ฑ
}
listMap.put("file_path",mf.getOriginalFilename());
listMap.put("real_file_path",dest);
mf.transferTo(dest);
}
return list;
}
DB ํ์ธ
~ ํ์ผ ๋ค์ด๋ก๋ ์๋ ์ฐธ๊ณ ~
'js' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[jQuery] ํ์ผ ์ ๋ก๋ input type = 'file' ๋ค์ค์ผ๋ก ์์ฑํ๊ธฐ (2) | 2021.10.26 |
---|---|
[jQuery] ๋์ ์ถ๊ฐ๋ append์ click event ์ฒ๋ฆฌ (0) | 2021.10.26 |
[JavaScript] .js ํ์ผ์์ contextPath ๊ตฌํ์ฌ ์ฌ์ฉํ๊ธฐ (sessionStorage) (0) | 2021.09.23 |
[jQuery] ajax success html์์ ์ํ๋ ์์ ์ฐพ๊ธฐ (0) | 2021.09.15 |
[jQuery] html() ์ append() ์ฐจ์ด (0) | 2021.09.15 |