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

复制代码
复制代码
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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值