欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jQuery利用FormData上傳文件實現(xiàn)批量上傳

 更新時間:2018年12月04日 14:10:38   作者:believe__sss  
這篇文章主要為大家詳細介紹了jQuery利用FormData上傳文件實現(xiàn)批量上傳功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下

在項目中涉及題庫的批量上傳功能,在此利用formdata進行文件上傳,后臺讀取,進行批量插入。同時還需要帶入teacherId和courseId兩個參數(shù),所以將文件和兩個參數(shù)append到formdata中,傳到后臺。

JQuery 函數(shù)的提交按鈕執(zhí)行的函數(shù)如下:

<script type="text/javascript">
 //批量上傳題庫
 function fileSubmit() {
 var questionFile = new FormData();
 var fileObj = document.getElementById("questionFile").files[0];
        // js 獲取文件對象,questionFile為文件選擇框的Id
 questionFile.append("file", fileObj);
 var teacherId=localStorage.getItem("teacherId");
 questionFile.append("teacherId",teacherId);
 var courseId=localStorage.getItem("courseId");
 questionFile.append("courseId",courseId);
 $.ajax({
  async: false,
  type:"post",
  url:"/questions/batchUpload",
  data:questionFile,
  processData : false, //必須false才會避開jQuery對 formdata 的默認處理
  contentType : false, //必須false才會自動加上正確的Content-Type
  success:function (data) {
  layer.msg("上傳成功");
  example.ajax.reload();
  }
 });
 }
 
</script>

需要注意的是以下兩點:

  • jQuery 的 ajax 中processData設置為false (表示不需要對數(shù)據(jù)做處理)
  • jQuery 的 ajax 中contentType設置為false (因為前面已經聲明了是‘FormData對象')

Controller 中的方法如下:

@ApiOperation(value = "批量上傳題庫")
 @RequestMapping(value = "/batchUpload",method = RequestMethod.POST)
 public void batchUploadQuestions(HttpServletRequest request) throws Exception{
 Collection<Part> files = request.getParts();
 questionsService.batchUploadQuestions(files);
 }

Service中的方法如下:

//題庫的批量上傳
 @Override
 public void batchUploadQuestions(Collection<Part> files) throws Exception {
 Iterator<Part> 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<Questions> questionsList = new ArrayList<>();
 //判斷Excel中有幾張表,目前設定為一張表
 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<String> strList = new ArrayList<>();
  for (int i = 1; i < row.getLastCellNum(); i++) {
        //獲取到一列,第一列為序號不需要存入數(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("")) {//此處設計不需要讀入的單元格
   strList.clear();
   break;
  }
  strList.add(value);
  }
  if (strList.size() == 9) {
         //對應數(shù)據(jù)庫屬性進行存儲
  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);
  }
 }
 
    //將前臺存進的teacherId也當做文件進行讀取
 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;
 }
    
     //將前臺傳入的courseId當做文件讀取
 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層進行數(shù)據(jù)插入
 public void batchSaveQuestionList(String teacherId,String courseId,List<Questions> questionsList){
 String sql = "replace into questions(questionId,courseId,teacherId,chapter,section,type,question,answerA,answerB,answerC,answerD,answerTrue) values";
 for(int i = 0;i<questionsList.size();i++){
  String questionId = String.valueOf(System.currentTimeMillis())+i;
  if(i==0){
  sql+="('"+questionId+"','"+courseId+"','"+teacherId+"','"+questionsList.get(i).getChapter()+"','"+questionsList.get(i).getSection()
   + "','"+questionsList.get(i).getType()+"','"+questionsList.get(i).getQuestion()+ "','"+ questionsList.get(i).getAnswerA()
   +"','"+questionsList.get(i).getAnswerB()+"','"+questionsList.get(i).getAnswerC()+"','"+questionsList.get(i).getAnswerD()
   +"','"+questionsList.get(i).getAnswerTrue()+"')";
  }else{
  sql+=",('"+questionId+"','"+courseId+"','"+teacherId+"','"+questionsList.get(i).getChapter()+"','"+questionsList.get(i).getSection()
   + "','"+questionsList.get(i).getType()+"','"+questionsList.get(i).getQuestion()+ "','"+ questionsList.get(i).getAnswerA()
   +"','"+questionsList.get(i).getAnswerB()+"','"+questionsList.get(i).getAnswerC()+"','"+questionsList.get(i).getAnswerD()
   +"','"+questionsList.get(i).getAnswerTrue()+"')";
  }
 }
 questionsDao.batchSaveQuestionList(sql);
 }

DAO層的數(shù)據(jù)插入語句:

@Insert("${sql}")
void batchSaveQuestionList(@Param("sql") String sql);

自此即可實現(xiàn)批量上傳,需要注意的是,這里定義的文件類型為Part類型。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家,關注腳本之家公眾號的更多精彩內容。

相關文章

  • jQuery實現(xiàn)字體顏色漸變效果的方法

    jQuery實現(xiàn)字體顏色漸變效果的方法

    這篇文章主要介紹了jQuery實現(xiàn)字體顏色漸變效果的方法,結合實例形式分析了jQuery動態(tài)設置元素屬性實現(xiàn)字體顏色漸變效果的相關操作技巧,需要的朋友可以參考下
    2017-03-03
  • jQuery控制文本框只能輸入數(shù)字和字母及使用方法

    jQuery控制文本框只能輸入數(shù)字和字母及使用方法

    這篇文章主要介紹了jQuery控制文本框只能輸入數(shù)字和字母及使用方法的相關資料,非常不錯而且實用性也很高,需要的朋友可以參考下
    2016-05-05
  • jQuery插件開發(fā)詳細教程

    jQuery插件開發(fā)詳細教程

    這篇文章主要介紹了jQuery插件開發(fā)詳細教程,將概述jQuery插件開發(fā)的基本知識,最佳做法和常見的陷阱,需要的朋友可以參考下
    2014-06-06
  • jQuery實現(xiàn)簡單的抽獎游戲

    jQuery實現(xiàn)簡單的抽獎游戲

    這篇文章主要為大家詳細介紹了jQuery實現(xiàn)簡單的抽獎游戲,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • jQuery中show與hide方法用法示例

    jQuery中show與hide方法用法示例

    這篇文章主要介紹了jQuery中show與hide方法用法,結合實例形式分析了jQuery使用show與hide方法控制頁面元素顯示與隱藏的簡單實現(xiàn)技巧,需要的朋友可以參考下
    2016-09-09
  • jQuery實現(xiàn)動畫效果circle實例

    jQuery實現(xiàn)動畫效果circle實例

    這篇文章主要介紹了jQuery實現(xiàn)動畫效果circle的方法,涉及jquery鼠標事件及頁面動畫操作相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-08-08
  • JQuery的Validation插件中Remote驗證的中文問題

    JQuery的Validation插件中Remote驗證的中文問題

    前段時間,再次出現(xiàn)AJAX中文編碼問題,導致會員名重復檢測失敗,不過這次出現(xiàn)問題的是Validation插件的remote驗證。
    2010-07-07
  • jQuery彈出層插件簡化版代碼

    jQuery彈出層插件簡化版代碼

    這個簡化版不能在別的框架彈出層的,所以也就沒有了那個cssurl屬性了,也沒有target 屬性了
    2008-10-10
  • jQuery表單插件ajaxForm實例詳解

    jQuery表單插件ajaxForm實例詳解

    前端時間寫項目用到了ajaxForm這個插件,可以用它提交表單和上傳圖片,聽起來和正常的form表單提交沒什么區(qū)別,只不過是ajax提交,無需刷新頁面,下面通過實例給大家介紹jQuery表單插件ajaxForm,需要的朋友參考下吧
    2017-01-01
  • jQuery使用toggleClass方法動態(tài)添加刪除Class樣式的方法

    jQuery使用toggleClass方法動態(tài)添加刪除Class樣式的方法

    這篇文章主要介紹了jQuery使用toggleClass方法動態(tài)添加刪除Class樣式的方法,實例分析了jQuery中toggleClass方法操作class樣式的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-03-03

最新評論