public ActionResult UploadAccountData(IEnumerable<HttpPostedFileBase> files) { // Only One file should be passed to server side--- validated in client upload // File upload control has validated to take single file and file types are .csv and .xlsx var returnResult = new UploadResult(); try { // The Name of the Upload component is "files" if (files != null) { foreach (var file in files) { var extension = Path.GetExtension(file.FileName); if (extension == ".xlsx") { // Save files on appropriate path var directory = Server.MapPath("~/Files/Uploaded/Excel/"); if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } var physicalPath = Path.Combine(directory, file.FileName); file.SaveAs(physicalPath); //Read and Save using SQL BULK Copy returnResult = ReadAndSaveAccountData(physicalPath, extension); } else if (extension == ".csv") { // Save files on appropriate path var directory = Server.MapPath("~/Files/Uploaded/CSV/"); if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } var physicalPath = Path.Combine(directory, file.FileName); file.SaveAs(physicalPath); //Read and Save using SQL BULK Copy returnResult = ReadAndSaveAccountData(physicalPath, extension); } } } // Return an empty string to signify success return Content(JsonConvert.SerializeObject(returnResult), "application/json"); } catch (Exception e) { return Content(JsonConvert.SerializeObject(new ErrorResponseView(e)), "application/json"); } }
//--------------------------------------------// //------------------Private methods ------------------// private UploadResult ReadAndSaveAccountData(string physicalPath, string extension) { // Initialize reader and writer IFileReader fileReader = new FileReader(); IFileWriter filewriter = new FileWriter(ConfigurationManager.ConnectionStrings["BulkDataProcessDbConnection"].ConnectionString); var returnResult = new UploadResult(); if (extension == ".xlsx") { foreach (ExtractedDataDTO dto in fileReader.GetExcelFileData(physicalPath)) { //fileWriter.WriteChunkData(tbl, destinationTableName, Map()); filewriter.WriteChunkData(dto.ChunkDataTable, "AccountDatas", Map()); returnResult.SkippedLines = dto.SkippedLines; returnResult.ToatalLineRecordsProcessed = dto.ToatalLineRecordsProcessed; } } else if (extension == ".csv") { foreach (ExtractedDataDTO dto in fileReader.GetCSVFileData(physicalPath)) { //fileWriter.WriteChunkData(tbl, destinationTableName, Map()); filewriter.WriteChunkData(dto.ChunkDataTable, "AccountDatas", Map()); returnResult.SkippedLines = dto.SkippedLines; returnResult.ToatalLineRecordsProcessed = dto.ToatalLineRecordsProcessed; } } return returnResult; }