multiple实现文件批量上传


jsp页面:

 1 <form id="form" enctype="multipart/form-data">
 2                             <table class="layout-table formBox-table" id="selectTable">
 3                                 <tr>
 4                                     <th style="width:70px; text-align:right">起始日期</th>
 5                                     <td style="width:100px;">
 6                                         <input name="begin_date" id="begin_date" class="" value=""/>
 7                                     </td>
 8                                     <th style="width:70px; text-align:right">截止日期</th>
 9                                     <td style="width:100px;">
10                                         <input name="end_date" id="end_date" value=""/>
11                                     </td>
12                                     <th style="width:70px; text-align:right">文件名</th>
13                                     <td style="width:100px;">
14                                         <input name="file_name" id="file_name" value=""/>
15                                     </td>
16                                     <td>
17                                         <input type="file" name="upload"  id="file" value="" multiple="multiple"/>
18                                         <input type="button" class="blueBtn" id="search" style="width:79px" value="查询"/>
19                                         <input type="button" class="blueBtn" id="importView" style="width:79px" value="导入"/>
20                                     </td>
21                                 </tr>
22                             </table>
23                         </form>

 

js:

 1 //导入
 2     $("#importView").click(function(){
 3         var path=$("body").find("input[name='upload']").val();
 4         var files=document.getElementById("file").files;
 5 
 6         if(files.length>0){
 7             var isDoc=true;
 8             for(var i=0;i<files.length;i++){
 9                 var name=files[i].name;
10                 if(name.substr(name.indexOf("."))!=".doc"){
11                     isDoc=false;
12                 }
13             }
14             if(isDoc){
15                 var form=document.forms[0];
16                 form.action=basePath+"dataDownload/importWord.spring";
17                 form.method="post";
18                 form.submit();
19             }else{
20                 $.ligerDialog.warn("请选择.doc文件");
21                 return;
22             }
23             
24             
25         }else{
26             $.ligerDialog.warn("请选择你需要导入的文件");
27             return;
28         }
29     })

 

 java后台:

 1 // 导入
 2     @ResponseBody
 3     @RequestMapping(value = "/dataDownload/importWord", produces = "text/html;charset=UTF-8")
 4     public ModelAndView importWord(@RequestParam List<MultipartFile> upload, HttpServletRequest request)
 5             throws Exception {
 6 //        filePath = new String(filePath.getBytes("iso-8859-1"), "utf-8");
 7         HttpSession session = request.getSession();
 8         Admin admin = (Admin) session.getAttribute("admin");
 9         String name = admin.getUserName();
10         List<String> errorList=new ArrayList<>();
11         for (MultipartFile mul : upload) {
12                 // 文件先上传到服务器中
13                 String filePath = request.getSession().getServletContext().getRealPath("/modelplace");
14                 File file = new File(filePath);
15                 if (!file.exists()) {
16                     file.mkdir();
17                 }
18                 String filename = mul.getOriginalFilename().substring(0, mul.getOriginalFilename().indexOf("."));
19                 File file1 = new File(filePath, filename + ".doc");
20                 if (!file1.exists()) {
21                     file1.createNewFile();
22                 }
23  mul.transferTo(file1);//此处已完成单个文件的上传操作,循环完成即可上传批量文件
24                29         }31         ModelAndView mv = new ModelAndView("dataDownload/lpgImport");
32         return mv;
33     }

注:红色标记处是自己认为比较关键的地方

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM