码迷,mamicode.com
首页 > 编程语言 > 详细

java jfinal + ajaxfileupload.js 上传

时间:2016-06-17 17:04:59      阅读:344      评论:0      收藏:0      [点我收藏+]

标签:

功能上传

前端: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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!