예제 #1
0
        public async Task <IActionResult> Import(IFormFile file)
        {
            if (file == null)
            {
                return(Content("Error: File is null"));
            }
            string fileExtension = Path.GetExtension(file.FileName);

            if (fileExtension != ".xls" && fileExtension != ".xlsx")
            {
                return(Content("Error: Wrong file extension"));
            }
            var             stream    = file.OpenReadStream();
            List <Contract> contracts = await Task.Run(() =>
            {
                ExcelHandler excelHandler = new ExcelHandler(stream, fileExtension);
                return(excelHandler.GetContractsFromExcel());
            });

            DBContext dBContext = new DBContext();
            await dBContext.Contracts.AddRangeAsync(contracts);

            await dBContext.SaveChangesAsync();

            return(Content("All good"));
        }