标签:
功能上传
前端:jquery.ajaxfileupload.js
后端:jfinal
upload.htm
<html>
<body>
<div class="form-element-file-wapper">
<button type="button">上传照片</button>
<input type="file" id="image0" name="image" accept="image/jpeg,image/png,image/gif">
</div>
<script>
$(document).ready(function(){
$("#image0").ajaxfileupload({
‘action‘: ‘/upload/uploadImage‘,
‘onComplete‘: function(data) {
var html ="";
html += "<img alt=‘‘ src=‘"+data.filepath+"‘>";
var name = "img"+$.trim(index);
html += "<input type=‘hidden‘ name="+name+" value=‘"+data.filepath+"‘>";
$("#img"+index).html(html);
},
‘onStart‘: function() {
},
‘onCancel‘: function() {
},
valid_extensions:[‘jpeg‘,‘png‘,‘gif‘,‘jpg‘]
});
});
</script>
</html>
</body>
Upload.java
public void uploadImage(){
UUID uuid = UUID.randomUUID();
String sysPath = getSession().getServletContext().getRealPath("");
File tempFile = new File(sysPath+"/upload/temp/");
if(!tempFile.exists()){
tempFile.mkdirs();
}
UploadFile upfile = getFile("image");
if(upfile != null){
File file = upfile.getFile();
String fileName = uuid+"_"+file.getName();
String filepath = "/upload/temp/"+fileName;
file.renameTo(new File(sysPath+filepath));
setAttr("filepath", filepath); //返回文件存放路径给前台
}
renderJson();
}
java jfinal + ajaxfileupload.js 上传
标签:
原文地址:http://www.cnblogs.com/chenweichu/p/5594256.html