private static final Logger logger=LoggerFactory.getLogger(UploadController.class);
private static final String JSP_PAGE ="/view/uploads";
private String uploadFile (HttpServletRequest request,
String originalName, byte[] fileData) throws Exception{
UUID uid =UUID.randomUUID();
String savedName =uid.toString() +"_" +originalName;
File target =new File(UploadPath.path(request),savedName);
FileCopyUtils.copy(fileData, target);
return savedName;
}
@RequestMapping(value="/uploadiframe", method=RequestMethod.GET)
public String uploadFormIframe() {
return JSP_PAGE+"/uploadiframe";
}
@RequestMapping(value="/uploadiframe", method=RequestMethod.POST)
public String uploadFormIrame(HttpServletRequest request,
MultipartFile file, Model model) throws Exception{
logger.info("originalName :" + file.getOriginalFilename());
logger.info("size : " + file.getSize());
logger.info("contentType :" +file.getContentType());
String savedName =uploadFile(request, file.getOriginalFilename(), file.getBytes());
model.addAttribute("savedName", savedName);
return JSP_PAGE+"/uploadiframeResult";
}
uploadiframResult
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ page session="false" %>
<script>
var result ='${savedName}';
parent.addFilePath(result);
</script>
uploadiframe
<div>
<h5>업로드할 파일을 선택해 주세요.</h5>
<form action="/upload/uploadiframe" method="post"
enctype="multipart/form-data" role='form1' target="zeroFrame" id="form1">
<input type="file" name="file" class="form-control" id="fileName"> <br>
<input type="submit" value="업로드" class="form-control" id="fileSubmit" >
</form>
<h5 id="savedName"></h5>
<iframe name="zeroFrame" style="width: 0px; height: 0px; border: 0px;"></iframe>
</div>
자바스크립트.
<script>
$(document).ready(function(){
$("#fileSubmit").click(function(event){
event.preventDefault();
var form=$("form[role='form1']");
var maxSize = 7340032; //7MB
if($('#fileName').val().length < 1){
alert("파일을 선택 해 주세요!");
return false;
}
if ($('#fileName').val() != "" ) {
var size = document.getElementById("fileName").files[0].size;
if(size > maxSize){
alert("첨부파일 사이즈는 7MB 이내로 등록 가능합니다.");
$('#fileName').val("");
$('#fileName').focus();
return false;
}
}
form.submit();
});
});
function addFilePath(msg){
alert(msg);
document.getElementById("form1").reset();
$("#savedName").html(msg);
}
</script>
댓글 ( 4)
댓글 남기기