博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
点击上传按钮,文件自动上传
阅读量:6901 次
发布时间:2019-06-27

本文共 3994 字,大约阅读时间需要 13 分钟。

1 2 3 4 

 

js代码:必须引入这两个js。第二个可以在网上下载

<script type="text/javascript" src="jquery/jquery-1.8.3.js"></script>

<script type="text/javascript" src="js/ajaxfileupload.js"></script>

1         function ajaxFileUpload(obj,ids) { 2             var file = document.getElementById("upload"+ids); 3             var fileName = document.getElementById("up"+ids); 4        9             $.ajaxFileUpload({10                 url : '/abs/uploadfile.html', //用于文件上传的服务器端请求地址11                 secureuri : false, //是否需要安全协议,一般设置为false12                 fileElementId : 'upload'+ids, //文件上传域的ID13                 dataType : 'JSON', //返回值类型 一般设置为json14                 data : {'params':ids,'basic.ID':id}, //返回值类型 一般设置为json15                 success : function(data) { //服务器成功响应处理函数16                     if(data.retcode="0"){17                         alert("上传成功!");18                         window.location.reload();19                     }else if(data.retcode="101"){20                         alert("上传失败!");21                     }else if(data.retcode="1"){22                         alert("上传出错!");23                     }24                 },25                 error : function(data) { //服务器响应失败处理函数26                     alert("Unknown Error!");27                 }28             });

action部分的代码:

1    private File upload;//必须与你的file文件上传域的name一致

   private String uploadContentType;

   private String uploadFileName;

  public String uploadFile(){  2         String filePath;  3         try {  4             if(upload==null){  5                 filePath=null;  6             }else{  7                 String uploadPath = ServletActionContext.getServletContext().getRealPath("/");  8                 filePath = "D:\\EPMS\\"+uploadFileName;  9                 FileInputStream fis = new FileInputStream(upload); 10                 FileOutputStream fos = new FileOutputStream(filePath); 11                 IOUtils.copy(fis, fos); 12                 fos.flush(); 13                 fis.close(); 14                 fos.close(); 15             }       //以上是上传的代码,下面为将上传的信息写入数据库 16             if(files==null){ 17                 files = new Files(); 18             } 19             System.out.println(fileid+"========"); 20             if(fileid!=null&&!"".equals(fileid)){ 21                 files.setID(fileid); 22                 files.setName(uploadFileName); 23                 files.setSrc(filePath); 24                 //files.setCreateTime(new Date()); 25                 files.setVersionNum(1); 26                  27                 exhiService.updateFile(files); 28             }else{ 29                 files.setID(UUID.randomUUID().toString()); 30                 files.setName(uploadFileName); 31                 files.setSrc(filePath); 32                 files.setCreateTime(new Date()); 33                 files.setVersionNum(1); 34                  35                 if("1".equals(params)){ 36                     files.setType("效果图"); 37                     files.setNoteID(shentu.getID()); 38                 } 39                 if("2".equals(params)){ 40                     files.setType("平面图"); 41                     files.setNoteID(shentu.getID()); 42                 } 43                 if("3".equals(params)){ 44                     files.setType("立面图"); 45                     files.setNoteID(shentu.getID()); 46                 } 87                  88                 exhiService.addFile(files); 89                 log.info("upload {} success!!!"); 90                 resultMap.put("retcode", RetCode.SUCCESS); 91                 resultMap.put("retmsg", "上传成功"); 92             } 93  94         } catch(Exception e){ 95             log.error("upload {} unknow_wrong!!!",e); 96             resultMap.put("retcode", RetCode.UNKOWN_WRONG); 97             resultMap.put("retmsg", "上传出错"); 98         } 99         return SUCCESS;100     }

 参考自博客:http://blog.csdn.net/yicong406880638/article/details/51473955

 有需要的也可以去这个地址看看,我就是看这个博客的才做出来的。

转载于:https://www.cnblogs.com/heyuxiu/p/6117800.html

你可能感兴趣的文章
中国网建之SMS
查看>>
《SQL 进阶教程》 case:练习题1-1-2 转换行列——在表头里加入汇总列
查看>>
展讯NAND Flash高级教程【转】
查看>>
DynamicPropertyAccessor Expression lambda
查看>>
csc.rsp Invent by Microshaoft
查看>>
CentOS7上GitHub/GitLab多帐号管理SSH Key
查看>>
字符串+变量
查看>>
style
查看>>
Log4j配置详解
查看>>
myql 查询树形表结果:说说、说说的述评、评论的回复
查看>>
java基础讲解02-----eclipse快捷方式(2017-04-12 23:47)
查看>>
Eclipse使用Tomcat发布项目时出现YadisException异常解决方案
查看>>
How to do Mathematics
查看>>
[洛谷P4245]【模板】任意模数NTT
查看>>
iOS开发-面试总结(十六)
查看>>
[LeetCode] Unique Binary Search Trees II dfs 深度搜索
查看>>
升级Mac OS X上的git
查看>>
python基础2(数据类型、数据运算、for循环、while循环、列表)
查看>>
应用被强杀了怎么办
查看>>
jquery validate 插件使用心得
查看>>