how to return export path without using copy to memory2 ?

ahmed salah 3,126 Reputation points
2021-09-07T23:58:29.27+00:00

I working on web api asp.net 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))
                 {
                     stream.CopyTo(memory2);
                 }
                 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

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
2,541 questions
C#
C#
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
    2021-09-08T00:12:33.127+00:00

    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
    2021-09-08T03:09:54.877+00:00

    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