public bool SaveLicence(LicensesDTO licenses) { try { var result = _licenseRepository.AddLicense(licenses.Licenses.ToList()); return(result); } catch (Exception ex) { throw new Exception("Error while saving license : " + ex.StackTrace); } }
public async Task <IActionResult> CreateLicense(IFormFile file) { if (file == null || file.Length == 0) { ViewBag.ErroMessage = "File Not Selected"; return(View()); } string rootPath = this._hostingEnvironment.ContentRootPath; string path = Path.Combine(this._hostingEnvironment.WebRootPath, "UploadedFile"); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } string fileName = Path.GetFileName(file.FileName); string fileExt = Path.GetExtension(fileName); var result = new StringBuilder(); using (FileStream fs = new FileStream(Path.Combine(path, fileName), FileMode.OpenOrCreate)) { if (System.IO.File.Exists(path + "\\" + fileName)) { System.IO.File.Delete(fileName); } await file.CopyToAsync(fs); } var fileToRead = path + "\\" + Path.GetFileName(file.FileName); string fileContext = await System.IO.File.ReadAllTextAsync(fileToRead); var licenseModel = new List <LicensesDTO>(); if (fileExt == ".txt") { foreach (var row in fileContext.Split('\n')) { if (!string.IsNullOrEmpty(row)) { var modelData = new LicensesDTO { Id = Convert.ToInt32(row.Split('\t')[0]), Name = row.Split('\t')[0], LicenseVal = Convert.ToByte(row.Split('\t')[0]) }; licenseModel.Add(modelData); } } } if (fileExt == ".csv") { foreach (var row in fileContext.Split('\n')) { if (!string.IsNullOrEmpty(row)) { var modelData = new LicensesDTO { Id = Convert.ToInt32(row.Split(',')[0]), Name = row.Split(',')[0], LicenseVal = Convert.ToByte(row.Split(',')[0]) }; licenseModel.Add(modelData); } } } var licenseSavedResponse = _licenseService.SaveLicence(licenseModel); ViewBag.SuccessMessage = "File Uploaded Successfully"; return(View()); }