private void OnChangedDirectory(object sender, FileSystemEventArgs e) { var fileName = e.FullPath; var userId = _dbContext.Users.FirstOrDefault(s => s.NormalizedUserName == "INTERNAL").Id; ILogger _logger = _loggerFactory.CreateLogger("Directory observer"); string error = ""; // filter file types if (Regex.IsMatch(fileName, @"\.xml|\.xlsx", RegexOptions.IgnoreCase)) { _logger.LogWarning($"Finded new file for inport: {fileName}"); try { if (fileName.ToLower().EndsWith(".xml")) { error = ImportFile.ImportXml(fileName, (new FileInfo(fileName)).Name, _dbContext, _logger, Configuration.GetSection("AppConfigurations").GetValue <string>("ImportedExcelArchive", ""), userId, null); } if (fileName.ToLower().EndsWith(".xlsx")) { error = ImportFile.ImportExcel(fileName, (new FileInfo(fileName)).Name, _dbContext, _logger, Configuration.GetSection("AppConfigurations").GetValue <string>("ImportedExcelArchive", ""), userId, null); } } catch (Exception ex) { _logger.LogError($"Import {fileName} failed. {ex.Message}. {ex.StackTrace}"); } } }
async public Task <IActionResult> Upload(List <IFormFile> files) { string error = ""; try { // full path to file in temp location var filePath = Path.GetTempFileName(); var user = await _userManager.FindByNameAsync(User.Identity.Name); foreach (var formFile in files) { if (formFile.Length > 0) { var stream = new FileStream(filePath, FileMode.Create); await formFile.CopyToAsync(stream); stream.Close(); if (formFile.FileName.ToLower().EndsWith(".xml")) { error = ImportFile.ImportXml(filePath, formFile.FileName, _db, _logger, _appConfig.Value.ImportedExcelArchive, user.Id); } else if (formFile.FileName.ToLower().EndsWith(".xlsx")) { error = ImportFile.ImportExcel(filePath, formFile.FileName, _db, _logger, _appConfig.Value.ImportedExcelArchive, user.Id); } else { error = "You can upoad only *.xml or *.xlsx files!!!"; } } } } catch (Exception ex) { _logger.LogError(Utils.GetFullError(ex)); error = "Internal server error"; } ViewBag.Error = error; ViewBag.PortletTitle = "Upload file result"; return(View()); }