how to return export path without using copy to memory2 ?

ahmed salah 3,216 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.
4,137 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.
10,198 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. AgaveJoe 26,181 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));
    
    1 person found this answer helpful.
    0 comments No comments

  2. Viorel 111.8K 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.

    0 comments No comments