Jquery+SpringMVC實現上傳Excel檔案,並批量匯入
阿新 • • 發佈:2019-01-05
1.前端程式碼
function alertUploadFile(){
//建立表單 var formData = new FormData(); var file = $("#upload").val(); console.log(file); var arr=file.split('\\');//注split可以用字元或字串分割 var fileName=arr[arr.length-1];//這就是要取得的圖片名稱 //var name = $("input").val(); formData.append("file",$("#upload")[0].files[0]); //formData.append("name",name); if ($('#upload').val() == "") { alert("請選擇所要上傳的檔案"); }else{ var index = file.lastIndexOf("."); if(index < 0 ){ alert("上傳的檔案格式不正確,請選擇Excel檔案(*.xls)!"); }else{ var ext = file.substring(index + 1, file.length); if(ext == "xls" || ext == "xlsx"){ //$.blockUI({ message: '<image src='image/loading.gif'></image><br><h3>正在載入中</h3>' }); startUploadFile(formData,fileName); }else{ console.log("100"); alert("上傳的檔案格式不正確,請選擇Excel檔案(*.xls)!"); } } } } function startUploadFile(formData,fileName){ $.blockUI.defaults.message = "<image src='image/loading.gif'></image><br><h3>正在載入中</h3>";//設定遮罩層樣式 //$(document).ajaxStart($.blockUI).ajaxStop($.unblockUI); $.blockUI();//開始遮罩 //開始上傳 $.ajax({ url : tokenUrl+"fileUpload2", type : 'POST', data : formData, // 告訴jQuery不要去處理髮送的資料 processData : false, // 告訴jQuery不要去設定Content-Type請求頭 contentType : false, beforeSend:function(request){ request.setRequestHeader("token", token); console.log("正在進行,請稍候"); } }).done(function(result) { $.unblockUI() ;//解除遮罩 var reusltData = JSON.parse(result); console.log(reusltData); //var staffList = ; console.log(reusltData.msg); localStorage.setItem("fileName",fileName); localStorage.setItem("reusltData", JSON.stringify(reusltData)); clearFileInput(); //呼叫utils.js下的方法,開啟新視窗 // openNewWinByJs("uploadExcelResult.html?tag=1&fileName="+fileName+""); //openNewWin("uploadExcelResult.html?tag=1&fileName="+fileName+""); //console.log(json); //alert("上傳成功"); /*layer.close(index); */ }, 'json').fail(function() { //網路錯誤 $.unblockUI() console.log("網路錯誤"); localStorage.setItem("fileName",fileName); clearFileInput(); openNewWinByJs("uploadExcelResult.html?tag=0&fileName="+fileName+""); //openNewWin("uploadExcelResult.html?tag=0&fileName="+fileName+""); }); }
2.後端程式碼
@RequestMapping("fileUpload2") public void fileUpload2(@RequestParam("file") CommonsMultipartFile file) throws IOException { long startTime=System.currentTimeMillis(); System.out.println("fileName:"+file.getOriginalFilename()); String path = ""; SystemParameter systemParameter = new SystemParameterDao().load("企業端excel上傳路徑"); if(systemParameter == null){ //path="C:\\Users\\fcr\\Desktop\\testupload"; path = "G:\\upload"; System.out.println("你好啊"); }else{ path = systemParameter.getValue(); System.out.println("-------"); } String filePath=path+"/"+new Date().getTime()+file.getOriginalFilename(); File newFile=new File(filePath); System.out.println("準備了"); //通過CommonsMultipartFile的方法直接寫檔案(注意這個時候) file.transferTo(newFile); System.out.println("要開始了"); long endTime=System.currentTimeMillis(); System.out.println("方法二的執行時間:"+String.valueOf(endTime-startTime)+"ms"); Map<String, Object> resultMap = customerEmployeeService.batchImportStaff(getCustomer(),filePath); returnSuccessJson(resultMap, false); }