how to return export path without using copy to memory2 ?

ahmed salah 3,126 Reputation points

I working on web api core 3.1 I need to return export path direct without copying to memory so How to do that Please ?

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))
                 memory2.Position = 0;

                 return File(memory2, "text/plain", Path.GetFileName(exportPath));

How to return export path without copy to memory as below

return File(memory2, "text/plain", Path.GetFileName(exportPath));

meaning using exportPath instead of using memory2

A set of technologies in the .NET Framework for building web applications and XML web services.
2,541 questions
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
6,944 questions
No comments
{count} votes

2 answers

Sort by: Most helpful
  1. AgaveJoe 17,096 Reputation points

    I think you are looking for the following????

    return File(System.IO.File.ReadAllBytes(exportPath), "text/plain", System.IO.Path.GetFileName(exportPath));
    No comments

  2. Viorel 82,646 Reputation points

    Check this approach too:

    ā€ƒreturn File(exportPath, "text/plain", Path.GetFileName(exportPath));

    However, if exportPath is a temporary file, you cannot delete it immediately.

    No comments