這篇文章將為大家詳細(xì)講解有關(guān)怎么在jQuery中使用FormData上傳文件,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
10年積累的成都網(wǎng)站制作、成都做網(wǎng)站經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站設(shè)計(jì)后付款的網(wǎng)站建設(shè)流程,更有懷化免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。jquery是什么jquery是一個(gè)簡(jiǎn)潔而快速的JavaScript庫,它具有獨(dú)特的鏈?zhǔn)秸Z法和短小清晰的多功能接口、高效靈活的css選擇器,并且可對(duì)CSS選擇器進(jìn)行擴(kuò)展、擁有便捷的插件擴(kuò)展機(jī)制和豐富的插件,是繼Prototype之后又一個(gè)優(yōu)秀的JavaScript代碼庫,能夠用于簡(jiǎn)化事件處理、HTML文檔遍歷、Ajax交互和動(dòng)畫,以便快速開發(fā)網(wǎng)站。
JQuery 函數(shù)的提交按鈕執(zhí)行的函數(shù)如下:
需要注意的是以下兩點(diǎn):
jQuery 的 ajax 中processData設(shè)置為false (表示不需要對(duì)數(shù)據(jù)做處理)
jQuery 的 ajax 中contentType設(shè)置為false (因?yàn)榍懊嬉呀?jīng)聲明了是‘FormData對(duì)象')
Controller 中的方法如下:
@ApiOperation(value = "批量上傳題庫") @RequestMapping(value = "/batchUpload",method = RequestMethod.POST) public void batchUploadQuestions(HttpServletRequest request) throws Exception{ Collectionfiles = request.getParts(); questionsService.batchUploadQuestions(files); }
Service中的方法如下:
//題庫的批量上傳 @Override public void batchUploadQuestions(Collectionfiles) throws Exception { Iterator it = files.iterator(); Part file = it.next(); Workbook workbook = null; if (file.getSubmittedFileName().endsWith("xlsx")) { workbook = new XSSFWorkbook(file.getInputStream()); } else if (file.getSubmittedFileName().endsWith("xls")) { workbook = new HSSFWorkbook(file.getInputStream()); } Cell cell = null; List questionsList = new ArrayList<>(); //判斷Excel中有幾張表,目前設(shè)定為一張表 Sheet sheet = workbook.getSheetAt(0);//獲取sheet表 for (int rowIndex = 2; rowIndex <= sheet.getLastRowNum(); rowIndex++) { //獲取到一行 Row row = sheet.getRow(rowIndex); if (row == null) { continue; } Questions questions = new Questions(); List strList = new ArrayList<>(); for (int i = 1; i < row.getLastCellNum(); i++) { //獲取到一列,第一列為序號(hào)不需要存入數(shù)據(jù)庫,所以從1開始讀 cell = row.getCell(i); String value = ""; switch (cell.getCellTypeEnum()) { case _NONE: break; case STRING: value = cell.getStringCellValue(); break; case NUMERIC: Pattern points_ptrn = Pattern.compile("0.0+_*[^/s]+"); if (DateUtil.isCellDateFormatted(cell)) {//日期 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); value = sdf.format(DateUtil.getJavaDate(cell.getNumericCellValue())); } else if ("@".equals(cell.getCellStyle().getDataFormatString()) || "General".equals(cell.getCellStyle().getDataFormatString()) || "0_".equals(cell.getCellStyle().getDataFormatString())) { //文本 or 常規(guī) or 整型數(shù)值 DecimalFormat df = new DecimalFormat("0"); value = df.format(cell.getNumericCellValue()); } else if (points_ptrn.matcher(cell.getCellStyle().getDataFormatString()).matches()) {//正則匹配小數(shù)類型 value = String.valueOf(cell.getNumericCellValue());//直接顯示 } break; default: value = cell.toString(); } if ((i == 2 || i == 3) && value.equals("")) {//此處設(shè)計(jì)不需要讀入的單元格 strList.clear(); break; } strList.add(value); } if (strList.size() == 9) { //對(duì)應(yīng)數(shù)據(jù)庫屬性進(jìn)行存儲(chǔ) questions.setChapter(strList.get(0)); questions.setSection(strList.get(1)); questions.setType(strList.get(2)); questions.setQuestion(strList.get(3)); questions.setAnswerA(strList.get(4)); questions.setAnswerB(strList.get(5)); questions.setAnswerC(strList.get(6)); questions.setAnswerD(strList.get(7)); questions.setAnswerTrue(strList.get(8)); questionsList.add(questions); } } //將前臺(tái)存進(jìn)的teacherId也當(dāng)做文件進(jìn)行讀取 Part file1 = it.next(); InputStream inputStream = file1.getInputStream(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); String line = null; String teacherId = ""; while ((line = bufferedReader.readLine()) != null) { teacherId = line; } //將前臺(tái)傳入的courseId當(dāng)做文件讀取 Part file2 = it.next(); InputStream inputStream1 = file2.getInputStream(); BufferedReader bufferedReader1 = new BufferedReader(new InputStreamReader(inputStream1)); String line1 = null; String courseId = ""; while ((line1 = bufferedReader1.readLine()) != null) { courseId = line1; } batchSaveQuestionList(teacherId, courseId, questionsList); } //SQL 語句拼接后傳入DAO層進(jìn)行數(shù)據(jù)插入 public void batchSaveQuestionList(String teacherId,String courseId,List questionsList){ String sql = "replace into questions(questionId,courseId,teacherId,chapter,section,type,question,answerA,answerB,answerC,answerD,answerTrue) values"; for(int i = 0;i DAO層的數(shù)據(jù)插入語句:
@Insert("${sql}") void batchSaveQuestionList(@Param("sql") String sql);關(guān)于怎么在jQuery中使用FormData上傳文件就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
新聞名稱:怎么在jQuery中使用FormData上傳文件-創(chuàng)新互聯(lián)
當(dāng)前網(wǎng)址:http://weahome.cn/article/esiio.html