功能上传

需求:同时上传多张图片

前端: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 上传的更多相关文章

  1. 使用ajaxfileupload.js上传文件

    一直以来上传文件都是使用form表单上传文件,也看到过有人使用js上传文件,不过看起来蛮简单的也就没有怎么去理会.今天突然要使用这种方式上传文件,期间还遇到点问题.因此就记录下来,方便以后遇到这样的问 ...

  2. ajaxfileupload.js上传文件兼容IE7及以上版本

    要兼容IE789,要修改ajaxfileupload.js;要将此处的代码替换掉 if(window.ActiveXObject) { var io = document.createElement( ...

  3. jquery插件--ajaxfileupload.js上传文件原理分析

    英文注解应该是原作者写的吧~说实话,有些if判断里的东西我也没太弄明白,但是大致思路还是OK的. jQuery.extend({ createUploadIframe: function (id, u ...

  4. 【转】JQuery插件ajaxFileUpload 异步上传文件(PHP版)

    前几天想在手机端做个异步上传图片的功能,平时用的比较多的JQuery图片上传插件是Uploadify这个插件,效果很不错,但是由于手机不支持flash,所以不得不再找一个文件上传插件来用了.后来发现a ...

  5. js 上传下载(留着备用)

      js 上传下载(留着备用) 下载文件 1. <a href="#" onClick="download()">下载文件</a>  & ...

  6. js上传文件带参数,并且,返回给前台文件路径,解析上传的xml文件,存储到数据库中

    ajaxfileupload.js jQuery.extend({ createUploadIframe: function(id, uri) { //create frame var frameId ...

  7. JQuery插件ajaxFileUpload 异步上传文件(PHP版)

    太久没写博客了,真的是太忙了.善于总结,进步才会更快啊.不多说,直接进入主题. 前几天想在手机端做个异步上传图片的功能,平时用的比较多的JQuery图片上传插件是Uploadify这个插件,效果很不错 ...

  8. MVC+AjaxFileUpload文件上传

    来源:微信公众号CodeL 本次给大家分享的是ajaxfileupload文件上传插件,百度一大堆功能超炫的文件上传插件,为什么我们会选择这个插件呢? 原因是在此之前,我们尝试使用过很多基于flash ...

  9. 【原创】用JAVA实现大文件上传及显示进度信息

    用JAVA实现大文件上传及显示进度信息 ---解析HTTP MultiPart协议 (本文提供全部源码下载,请访问 https://github.com/grayprince/UploadBigFil ...

随机推荐

  1. 未能加载文件或程序集“System.WEB.DataVisualization, Version=3.5.0.0, Culture=neutral

    项目打开 提示 如题错误. 最近用VS2010 + .NET Framework3.5SP1开发程序,使用了MsChart,但是部署到服务器的时候提示如下错误: 分析器错误消息: 未能加载文件或程序集 ...

  2. Dom操作html详细

    <p name='pn'>xxx</p> <p name='pn'>xxx</p> <p name='pn'>xxx</p> & ...

  3. 更新安装xcode7插件

    mkdir -p ~/Library/Application\ Support/Developer/Shared/Xcode/Plug-inscurl -fsSL https://raw.github ...

  4. Oracle普通索引,唯一索引,主键的区别

    索引是我们经常使用的一种数据库优化手段,适当的业务操作场景使用适当的索引方案,可以显著的提升系统整体查询性能,当然用户体验也随之提高. 在Oracle中,唯一性索引(Unique Index)是我们经 ...

  5. Canopy v. 1.5.5 ubuntu安装流程

    官网的下载超级慢,还总是断,一断就失败了 我花费了7个小时终于在尝试了5次以后下载成功了,现在将网盘链接分享出来 https://yunpan.cn/cxt28gM26mxQU  访问密码 301d ...

  6. iOS 在UILabel显示不同的字体和颜色

    转自:http://my.oschina.net/CarlHuang/blog/138363 在项目开发中,我们经常会遇到在这样一种情形:在一个UILabel 使用不同的颜色或不同的字体来体现字符串, ...

  7. Android如何分析和研究Monkey Log文件

    Log 在android中的地位非常重要,要是作为一个android程序员不能过分析log这关,算是android没有入门吧 . 下面我们就来说说如何处理log文件 . 什么时候会有Log文件的产生 ...

  8. 解决:insert Vodafone sim card,open the mms read report,when receive the read report,cann't download..

    insert Vodafone sim card,open the mms read report,when receive the read report,cann't download the m ...

  9. linux 任务调度 系统任务调度

    linux  at 针对运行一次的任务 crontab   控制计划任务的命令 crond系统服务 crond是linux下用来周期性的执行某种任务或等待处理某些事件的一个守护进程, 与windows ...

  10. Android布局居中的几种做法

    Android的布局文件中,如果想让一个组件(布局或View)居中显示在另一个布局(组件)中,可以由这么几种做法: android:layout_gravity android:gravity and ...