網上很多文章,但是回顯出問題,上傳有兩種格式,一種是異步,一種是同步,我用異步多文件上傳時,異步的請求到后台會多次調用上傳方法,就是有幾個文件就調用幾次,所以我用同步方式,一並處理,回調一次
前端頁面
<head> <meta name="viewport" content="width=device-width" /> <title>日訂單導入</title> <script src="~/Scripts/fileinput.js"></script> <script src="~/Scripts/zh.js"></script> <link href="~/Content/fileinput.css" rel="stylesheet" /> </head> <body> <div style="text-align:center"> <form> <div class="modal-title"> <h4>請選擇日訂單文件</h4> </div> <div class="modal-body"> <input type="file" name="txt_file" id="txt_file" multiple class="file-loading" /> </div> <button type="submit" class="btn btn-default">提交</button> </form> </div> </body> </html> <script type="text/javascript"> $(function () { var control = $("#txt_file"); var uploadrul = "/VW_Import/DayFileUpload"; control.fileinput({ language: 'zh', //設置語言 uploadUrl: uploadrul, //上傳的地址 allowedFileExtensions: ['xls','xlsx'],//接收的文件后綴 showUpload: true, //顯示批量上傳按鈕 showCaption: false,//是否顯示標題 browseClass: "btn btn-primary", //按鈕樣式 dropZoneEnabled: true,//是否顯示拖拽區域 uploadAsync: false, //默認異步上傳 showRemove: true, //顯示移除按鈕 //showPreview: true, //是否顯示預覽 showCaption: false,//是否顯示標題 maxFileCount: 100, enctype: 'multipart/form-data', validateInitialCount: true, previewFileIcon: "<i class='glyphicon glyphicon-file'></i>", msgFilesTooMany: "選擇上傳的文件數量({n}) 超過允許的最大數值{m}!", }); //導入文件上傳完成之后的事件 $("#txt_file").on("filebatchuploadsuccess", function (event, data, previewId, index) { result = $.parseJSON(data.response); if (result.success == true) { alert("導入成功!"); } else { alert(result.errorMsg); } }); }); </script>
后台代碼
public ActionResult DayFileUpload() { try { HttpFileCollection files = System.Web.HttpContext.Current.Request.Files; List<VW_DayParts> lst = new List<VW_DayParts>(); List<VW_DayPartsVersion> versionlst = new List<VW_DayPartsVersion>(); var checkList = dbVW_DayPartsVersion.Entities().ToList(); bool update = true; for (int j = 0; j < files.Count; j++) { HttpPostedFile PostedFile = files[j]; string fileName = PostedFile.FileName; //VW_DayPartsVersion s = checkList.Where(m => m.FileName.Contains(fileName)).FirstOrDefault(); if (checkList.Where(m => m.FileName.Contains(fileName)).FirstOrDefault() != null) { update = false; break; } string filePath = Server.MapPath("~/VW_Upload/DayOrder/" + fileName); PostedFile.SaveAs(filePath); IWorkbook workbook; //XSSFWorkbook hssfworkbook; string fileExt = Path.GetExtension(fileName).ToLower(); using (FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { if (fileExt == ".xlsx") { workbook = new XSSFWorkbook(file); } else if (fileExt == ".xls") { workbook = new HSSFWorkbook(file); } else { workbook = null; } //workbook = new XSSFWorkbook(file); } NPOI.SS.UserModel.ISheet sheet = workbook.GetSheetAt(1); //列數 int cellCount = sheet.GetRow(0).LastCellNum; //int cellCount = sheet.GetRow(0).LastCellNum; UserLoginInfo loginInfo = aService.GetUserLoginInfo(); VW_DayPartsVersion dVersion = new VW_DayPartsVersion(); dVersion.FileName = fileName; dVersion.UpTime = DateTime.Now; dVersion.UserId = loginInfo.UserId; string[] tempName = fileName.Split(' '); dVersion.Factory = tempName[1].Substring(0, 4); versionlst.Add(dVersion); for (int i = 1; i <= sheet.LastRowNum; i++) { //int j = 0;//0 3 4 17 18 VW_DayParts item = new VW_DayParts(); IRow currentRow = sheet.GetRow(i);// i=1 從第二行開始 //物料號 if (!string.IsNullOrWhiteSpace(currentRow.GetCell(0).ToString())) { item.MATNR = currentRow.GetCell(0).ToString(); } //工廠代碼 if (!string.IsNullOrWhiteSpace(currentRow.GetCell(3).ToString())) { item.Factory = currentRow.GetCell(3).ToString(); } //合同編號 if (!string.IsNullOrWhiteSpace(currentRow.GetCell(4).ToString())) { item.ContractNo = currentRow.GetCell(4).ToString(); } //交貨時間 if (!string.IsNullOrWhiteSpace(currentRow.GetCell(17).ToString())) { item.DeliveryDate = Convert.ToDateTime(currentRow.GetCell(17).ToString()); } //計划數量 if (!string.IsNullOrWhiteSpace(currentRow.GetCell(18).ToString())) { item.Quantity = Convert.ToInt32(currentRow.GetCell(18).NumericCellValue); } item.FileName = fileName; item.UpTime = DateTime.Now; item.UserId = loginInfo.UserId; lst.Add(item); } } if(update) { dbVW_DayParts.BulkInsert(lst); dbVW_DayPartsVersion.Insert(versionlst); return Json(MsgCommon.respJsonSuccessMsg()); } else { return Json(MsgCommon.respJsonErrorMsg("文件名重復或文件已上傳")); } } catch (DbEntityValidationException dbEx) { return Json(MsgCommon.respJsonErrorMsg(dbEx.ToString())); } }