jquery传输文件到后端,后端处理数据。
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                jquery传输文件到后端,后端处理数据。
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                
                            
                            
                            <!--HTML代碼-->
<input type="file" name="uploadFile" id="uploadFile">
<!--JQuery代碼-->
$("#uploadFile").on("change", function() {debugger;var obj=$("#uploadFile")[0].files;var formData = new FormData();                      // 創建一個form類型的數據formData.append('file',obj[0]);     // 獲取上傳文件的數據$.ajax({"url": "","type": "","processData": false, // 將數據轉換成對象,不對數據做處理,故 processData: false"contentType": false,    // 不設置數據類型"xhrFields": {                // 這樣在請求的時候會自動將瀏覽器中的cookie發送給后臺withCredentials: true},"data": formData,success: function(data) {console.log(data)},error: function(data) {}})})
 
                        
                        
                        后端代碼
// 取得上傳的文件MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;CommonsMultipartFile file = (CommonsMultipartFile) multipartRequest.getFile("file");// 得到文件名稱String realFileName = file.getOriginalFilename();String suffix = realFileName.substring(realFileName.indexOf("."),realFileName.length());Workbook workbook = null;//判斷文件類型(可忽略)if (".xlsx".equals(suffix)) {workbook = new XSSFWorkbook(file.getInputStream());} else {workbook = new HSSFWorkbook(file.getInputStream());}List<WGxZdywCrm> empList = getEmpList(workbook);message=JSONObject.fromObject(empList);后端可以將獲取的文件傳到服務器,也可以將文件內容解析出來。
文件內容解析(excel文件)放入對象
List<WGxZdywCrm> wGxZdywCrmList = new ArrayList<>();SimpleDateFormat dateFormatUtil=new SimpleDateFormat("yyyy-MM-dd hh-mm-ss");Sheet sheet = wookbook.getSheetAt(0);//統計excel的行數int rowLen = sheet.getPhysicalNumberOfRows();//excel總行數,記錄數=行數-1for (int i = 1; i < rowLen; i++) {WGxZdywCrm wGxZdywCrm = new WGxZdywCrm();Row row = sheet.getRow(i);int startCol = 0;if (row != null) {String fZdywSort = getValue(row.getCell(startCol++));//getValue下面貼出來了String fZdywCrmno = getValue(row.getCell(startCol++));String fZdywBusinesscode = getValue(row.getCell(startCol++));String fZdywContractno = getValue(row.getCell(startCol++));String fZdywCustomercount = getValue(row.getCell(startCol++));String fZdywPhonecount = getValue(row.getCell(startCol++));String fZdywAccepttime = getValue(row.getCell(startCol++));String fZdywFinishtime = getValue(row.getCell(startCol++));String fZdywNote = getValue(row.getCell(startCol++));wGxZdywCrm.setFZdywSort(Long.valueOf(fZdywSort));wGxZdywCrm.setFZdywCrmno(fZdywCrmno);wGxZdywCrm.setFZdywBusinesscode(fZdywBusinesscode);wGxZdywCrm.setFZdywContractno(fZdywContractno);wGxZdywCrm.setFZdywCustomercount(fZdywCustomercount);wGxZdywCrm.setFZdywPhonecount(fZdywPhonecount);wGxZdywCrm.setFZdywAccepttime(dateFormatUtil.parse(fZdywAccepttime));wGxZdywCrm.setFZdywFinishtime(dateFormatUtil.parse(fZdywFinishtime));wGxZdywCrm.setFZdywNote(fZdywNote);wGxZdywCrmList.add(wGxZdywCrm);}}return wGxZdywCrmList;獲取內容
private String getValue(Cell cell) {if (cell == null)return "";if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {return String.valueOf(cell.getBooleanCellValue()).trim();} else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {DecimalFormat df = new DecimalFormat("#");return String.valueOf(df.format(cell.getNumericCellValue())).trim();} else {cell.setCellType(cell.CELL_TYPE_STRING);return String.valueOf(cell.getStringCellValue()).trim();}}總結
以上是生活随笔為你收集整理的jquery传输文件到后端,后端处理数据。的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 【算法】蛮力法
- 下一篇: 大体重程序员的减肥经历(不完整待补充)
