public async Task <IActionResult> UploadFile(IFormFile file) { if (RouteData != null) { _tenant = (Tenant)RouteData.Values.SingleOrDefault(r => r.Key == "tenant").Value; } var username = await _userManager.GetUserAsync(User); if (file == null || file.Length == 0) { return(Content("file not selected")); } var dt = DateTime.Now; //repare path for file var path = Path.Combine( Directory.GetCurrentDirectory(), "wwwroot\\FileUploads" + "\\" + _tenant.HostName, DateTime.Now.ToString("d").Replace("/", "-") + "-" + GetRandomNumber(0, 10) + "-" + file.GetFilename()); //input excel file to system in order read it using (var stream = new FileStream(path, FileMode.Create)) { await file.CopyToAsync(stream); } FileInfo fileInfo = new FileInfo(path); //check whether the file is excel or not try { ExcelPackage package = new ExcelPackage(fileInfo); } catch { //if file is not excel file, delete it System.IO.File.Delete(path); TempData["Message"] = "Please upload excel file picture with *.xslx"; return(RedirectToAction("Index")); } using (ExcelPackage package = new ExcelPackage(fileInfo)) { List <Inspection> inspectionList = new List <Inspection>(); //count how many worksheet in excel file int workSheetTotal = package.Workbook.Worksheets.Count; try { //loop all worksheet in excel file for (int j = 1; j <= workSheetTotal; j++) { if (_tenant.TenantId == (int)TenantEnum.garmex) { _inspectionImportService.ImportItemByGarmexTenant(username, package, inspectionList, j); } else { _inspectionImportService.ImportItem(username, package, inspectionList, j); } } } catch (Exception ex) { if (System.IO.File.Exists(path)) { System.IO.File.Delete(path); } } _inspectionRepository.InsertList(inspectionList); return(RedirectToAction("Index")); } }