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