java jfinal + ajaxfileupload.js 上传
功能上传
需求:同时上传多张图片
前端: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>
//绑定6个按钮控件
$(document).ready(function(){
var arr = [0,1,2,3,4,5];
$(arr).each(function(index,element){
$("#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();
}
//保存文件和做相关业务
public void saveUploadImage(String filepath[],Business business){
String sysPath = getSession().getServletContext().getRealPath("");
File savepath = new File(sysPath+"/upload/image/");
if(!savepath.exists()){
savepath.mkdirs();
}
for(int i=0;i<filepath.length;i++){
File file = new File(sysPath+filepath[i]);
if(!file.exists()){
continue;
}
file.renameTo(new File(savepath+File.separator+file.getName()));
System.out.println(new File(savepath+File.separator+file.getName()).getAbsolutePath());
business.set("id","business_seq.nextval");
//其它字段...
business.set("head_img"+(i+1), "/upload/image/"+file.getName());
business.save();
}
}
二、更新
public void UpdateUploadImage(){
UUID uuid = UUID.randomUUID();
String sysPath = getSession().getServletContext().getRealPath("");
File savepath = new File(sysPath+"/upload/image/");
if(!savepath.exists()){
savepath.mkdirs();
}
UploadFile upfile = getFile("image");
if(upfile == null){
return;
}
Integer seq = getParaToInt("seq")+1;
Long businessId = getParaToLong("businessId");
//删除原来文件
Record record = BusinessService.findById(businessId);
String head_img = record.get("head_img"+seq);
if(head_img!=null && !"".equals(head_img)){
File f = new File(sysPath+head_img);
if(f.exists()){
f.delete();
}
}
//保存新文件路径
File file = upfile.getFile();
String fileName = uuid+"_"+file.getName();
String filepath = "/upload/student_store_image/"+fileName;
file.renameTo(new File(sysPath+filepath));
//更新数据库
Business business= new Business();
business.set("id", studentStoreId);
business.set("head_img"+seq, filepath);
business.update();
setAttr("filepath", filepath);
renderJson();
}
注释:
1、新增业务:先把图片存放在服务器的临时目录,待用户所有资料填完点击提交。保存资料和一次保存6张图片。
2、更新业务:直接保存图片到服务器目录,而且更新数据库记录,记录图片的目录位置。
3、新增:一次性提交所有form表单数据,当然包括图片。 更新:多次提交,普通表单数据的提交/数据文件的提交。
java jfinal + ajaxfileupload.js 上传的更多相关文章
- 使用ajaxfileupload.js上传文件
一直以来上传文件都是使用form表单上传文件,也看到过有人使用js上传文件,不过看起来蛮简单的也就没有怎么去理会.今天突然要使用这种方式上传文件,期间还遇到点问题.因此就记录下来,方便以后遇到这样的问 ...
- ajaxfileupload.js上传文件兼容IE7及以上版本
要兼容IE789,要修改ajaxfileupload.js;要将此处的代码替换掉 if(window.ActiveXObject) { var io = document.createElement( ...
- jquery插件--ajaxfileupload.js上传文件原理分析
英文注解应该是原作者写的吧~说实话,有些if判断里的东西我也没太弄明白,但是大致思路还是OK的. jQuery.extend({ createUploadIframe: function (id, u ...
- 【转】JQuery插件ajaxFileUpload 异步上传文件(PHP版)
前几天想在手机端做个异步上传图片的功能,平时用的比较多的JQuery图片上传插件是Uploadify这个插件,效果很不错,但是由于手机不支持flash,所以不得不再找一个文件上传插件来用了.后来发现a ...
- js 上传下载(留着备用)
js 上传下载(留着备用) 下载文件 1. <a href="#" onClick="download()">下载文件</a> & ...
- js上传文件带参数,并且,返回给前台文件路径,解析上传的xml文件,存储到数据库中
ajaxfileupload.js jQuery.extend({ createUploadIframe: function(id, uri) { //create frame var frameId ...
- JQuery插件ajaxFileUpload 异步上传文件(PHP版)
太久没写博客了,真的是太忙了.善于总结,进步才会更快啊.不多说,直接进入主题. 前几天想在手机端做个异步上传图片的功能,平时用的比较多的JQuery图片上传插件是Uploadify这个插件,效果很不错 ...
- MVC+AjaxFileUpload文件上传
来源:微信公众号CodeL 本次给大家分享的是ajaxfileupload文件上传插件,百度一大堆功能超炫的文件上传插件,为什么我们会选择这个插件呢? 原因是在此之前,我们尝试使用过很多基于flash ...
- 【原创】用JAVA实现大文件上传及显示进度信息
用JAVA实现大文件上传及显示进度信息 ---解析HTTP MultiPart协议 (本文提供全部源码下载,请访问 https://github.com/grayprince/UploadBigFil ...
随机推荐
- ODI中web service介绍
ODI WS架构
- ODI 12c 安装
软件下载地址: http://www.oracle.com/technetwork/middleware/data-integrator/downloads/index.html 下载这个版本: Or ...
- js 弹出div窗口 可移动 可关闭 (转)
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- oracle 备份和还原还有创建用户、表空间、授权
--找到存放dbf文件的路径--E:\oracle\product\10.2.0\oradata\orcl--可以通过此语句进行查询select * from v$datafile; --创建表空间c ...
- mysql innoDB 与 myISAM
转载文章 出处 http://www.pureweber.com/article/myisam-vs-innodb/ 使用MySQL当然会接触到MySQL的存储引擎,在新建数据库和新建数据表的时候都 ...
- poj蚂蚁问题
问题描述: n只蚂蚁以每秒1cm的速度在长为Lcm的竿子上爬行.当蚂蚁爬到竿子的端点时就会掉落.由于竿子太细,两只蚂蚁相遇时,它们不能交错通过,只能各自反向 爬回去.对于每只蚂蚁,我们知道它距离竿子左 ...
- java classpath深入详解(转)
http://developer.51cto.com/art/200509/2786.htm 设置类路径 结构 可通过对 JDK 工具使用 -classpath 选项(首选方法)或设置 CLASSPA ...
- 自定义UIAlertView
You can change accessoryView to any own customContentView in a standard alert view in iOS7 [alertVie ...
- Makefile总结和反序字符数组,整体右移数组,杨辉三角!
2015.1.23今天我值日 继续接着昨天Makefile的目标开始记录:第一种:有 .PHNOY声明,但是冒号后面没有依赖文件.PHNOY:clean clean://没有依赖文件 rm *.0 t ...
- iOS9的适配
1.大部分社交平台接口不支持https协议. 问题描述:在iOS9下,系统默认会拦截对http协议接口的访问,因此无法获取http协议接口的数据.对ShareSDK来说,具体表现可能是,无法授权.分享 ...