コード例 #1
0
        public async Task <IActionResult> ImportData(FileDTO fileDto)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    string messages = string.Join("; ", ModelState.Values
                                                  .SelectMany(x => x.Errors)
                                                  .Select(x => x.ErrorMessage));
                    throw new Exception("Please correct the following errors: " + Environment.NewLine + messages);
                }
                //Upload the Excel Spreadsheet of learners
                var path = _fileService.UploadFile(fileDto.File, _foldersConfigation.Uploads);

                //import the data
                if (path != null)
                {
                    await _dataService.ImportExcelForLearners(path);

                    _notyf.Success("File uploaded successfully...");
                }
                else
                {
                    _notyf.Error("Invalid file uploaded");
                    return(Json(new { Result = "ERROR", Message = "Invalid file uploaded" }));
                }
                return(Json(new { Result = "OK" }));
            }
            catch (Exception ex)
            {
                _notyf.Error("Something went wrong : " + ex.Message);
                return(Json(new { Result = "ERROR", Message = ex.Message }));
            }
        }