public IHttpActionResult Post() { ValidationResult validationResult; var filename = string.Empty; try { var httpRequest = HttpContext.Current.Request; if (httpRequest.Files.Count > 0) { var postedFile = httpRequest.Files[0]; if (!LogicApi.HasContents(postedFile)) { throw new Exception($"{postedFile.FileName} File doesn't have content!"); } var objectDefinitions = Shared.Definitions.GetProperties <Product>(); var validationHandler = LogicApi.GetValidator(postedFile.FileName, objectDefinitions); if (validationHandler == null) { throw new Exception("File format not supported!"); } // Save file in server if greater max content length if (postedFile.ContentLength > LogicApi.GetMaxContentLength()) { filename = LogicApi.CreateFileInServerFolder(postedFile); validationResult = validationHandler.ValidFile(filename, true, false); } // Upload file in memory else { var rows = LogicApi.ConvertContentsInRows(postedFile); validationResult = validationHandler.ValidFile(rows, true, false); } if (validationResult.ValidationError.Any(e => e.ErrorType == ErrorTypeDef.Fatal)) { return(Content(HttpStatusCode.BadRequest, new { error = true, message = "Fatal error found! The file was not upload!", exception = validationResult.ValidationError })); } SaveData(validationResult, objectDefinitions); } else { return(Content(HttpStatusCode.BadRequest, "File Not Found!")); } } catch (Exception e) { return(Content(HttpStatusCode.BadRequest, e.Message)); } finally { if (!string.IsNullOrEmpty(filename) && File.Exists(filename)) { try { File.Delete(filename); } catch (Exception e) { Console.WriteLine(e); throw; } } } return(Ok(HttpStatusCode.Created)); }