I work on asp.net core 2.2 web API using C# language
I need to rewrite function below with best syntax and with best practice
web API below get Excel file from upload and return Excel file
it working without any issue but I need to rewrite it with best syntax and practice
some points I need to changes :
concatenate path is best thing using
are streaming using correctly
are memory copying file is correct
[HttpPost, DisableRequestSizeLimit]
[Route("Upload")]
public IActionResult Upload()
{
try
{
var DisplayFileName = Request.Form.Files[0];
string fileName = DisplayFileName.FileName.Replace(".xlsx", "-") + Guid.NewGuid().ToString() + ".xlsx";
string Month = DateTime.Now.Month.ToString();
string DirectoryCreate = myValue1 + "\\" + Month + "\\" + fileName;
string exportDirectory = myValue2 + "\\" + Month;
string exportPath = myValue2 + "\\" + Month + "\\" + fileName;
string FinalPath = exportPath;
if (!Directory.Exists(DirectoryCreate))
{
Directory.CreateDirectory(DirectoryCreate);
}
if (!Directory.Exists(exportDirectory))
{
Directory.CreateDirectory(exportDirectory);
}
CExcel ex = new CExcel();
if (DisplayFileName.Length > 0)
{
var filedata = ContentDispositionHeaderValue.Parse(Request.Form.Files[0].ContentDisposition).FileName.Trim('"');
var dbPath = Path.Combine(DirectoryCreate, fileName);
using (var stream = new FileStream(dbPath, FileMode.Create))
{
Request.Form.Files[0].CopyTo(stream);
stream.Flush();
stream.Close();
}
GC.Collect();
string error = "";
int rowCount = 0;
string inputTemplatePath = "";
var InputfilePath = System.IO.Path.Combine(GetFilesDownload, "DeliveryGeneration_Input.xlsx");
bool areIdentical = ex.CompareExcel(dbPath, InputfilePath, out rowCount, out error);
if (areIdentical == true)
{
List<InputExcel> inputexcellist = new List<InputExcel>();
inputexcellist = ex.Import(dbPath);
List<string> mods = new List<string>();
mods = inputexcellist.Select(x => x.ModuleName).Distinct().ToList();
var OutputfilePath = System.IO.Path.Combine(GetFilesDownload, "DeliveryGeneration_Output.xlsx");
if (System.IO.Directory.Exists(Path.Combine(exportDirectory, fileName)))
{
throw new Exception("Ok so the error message IS right.");
}
System.IO.File.Copy(OutputfilePath, Path.Combine(exportDirectory, fileName), true);
SqlConnection con;
foreach (var m in mods)
{
List<InputExcel> inputmodulelist = new List<InputExcel>();
inputmodulelist = inputexcellist.Where(x => x.ModuleName == m).ToList();
var dtimport = DatatableConversion.ToDataTable(inputmodulelist);
DataTable dtexport = new DataTable();
dtexport = _deliveryService.LoadExcelToDataTable(_connectionString, dtimport);
ex.Export(dtexport, m, exportPath);
}
}
var memory2 = new MemoryStream();
using (var stream = new FileStream(exportPath, FileMode.Open))
{
stream.CopyTo(memory2);
}
memory2.Position = 0;
return File(memory2, "text/plain", Path.GetFileName(exportPath));
}
else
{
return BadRequest();
}
}
catch (Exception ex)
{
return StatusCode(500, $"Internal server error: {ex}");
}
}
Update original post
function above get excel file as input with some data
then I search on database for matched data input
then get matched data on database on output file
then on last download it with output data
I using name space
using ClosedXML.Excel;
using OfficeOpenXml;
DataTablefrom the database, not some metadata that could reference the actual file. please give us more details. \$\endgroup\$