Esempio n. 1
0
        public async Task <IActionResult> Post(List <IFormFile> files,
                                               bool isHeadedFiles = false)
        {
            try
            {
                // Check there is at least 1 file
                if (files != null && files.Count == 0)
                {
                    return(NotFound(new
                    {
                        message = "0 files received"
                    }));
                }

                // Data list of parsed files
                List <CsvHandleResult> resultList =
                    new List <CsvHandleResult>();

                foreach (var formFile in files)
                {
                    // Parse each file
                    var result = await _csvFileHandler.ParseCsvFile(
                        formFile, isHeadedFiles);

                    // Check if parsed successfully
                    if (!result.Success)
                    {
                        return(NotFound(new
                        {
                            message = result.ErrorMessage
                        }));
                    }

                    resultList.Add(result);
                }

                return(Ok(new
                {
                    isHeadedFiles,
                    resultList
                }));
            }
            catch (Exception e)
            {
                return(NotFound(new
                {
                    message = $"Something went wrong: {e.Message}"
                }));
            }
        }
Esempio n. 2
0
        public ActionResult Index(FileUploadModel fileUploadModel)
        {
            if (fileUploadModel?.File == null || fileUploadModel.File.ContentLength <= 0)
            {
                return(HandleError("You need to click Choose File first, then Submit."));
            }

            var result = _csvFileHandler.ParseCsvFile(fileUploadModel.File, fileUploadModel.ContainsHeader);

            if (!result.Success)
            {
                return(HandleError(result.ErrorMessage));
            }

            return(View("FormattedDisplay", new FormattedDisplayModel
            {
                OriginalFileName = fileUploadModel.File.FileName,
                CsvTable = result.ParsedCsvContent
            }));
        }