点击上传按钮,文件自动上传

1 <input type="button" class="button green" value="上传" onclick= "$('#upload1').click();">
2 <input type="button" class="button green" value="上传" onclick= "$('#upload2').click();">
3 <input type="file" id="upload1" name="upload" onchange="ajaxFileUpload(this,1)" style="display:none">
4 <input type="file" id="upload2" name="upload" onchange="ajaxFileUpload(this,2)" style="display:none">

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, //是否需要安全协议,一般设置为false
12                 fileElementId : 'upload'+ids, //文件上传域的ID
13                 dataType : 'JSON', //返回值类型 一般设置为json
14                 data : {'params':ids,'basic.ID':id}, //返回值类型 一般设置为json
15                 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