'How can i save a file that i create into a path

I´m developing a school project where i need to export a html table into a csv file, and when i click in a button i need to save that file into a path like C:\csvfolder in the server machine using asp net c#,

The code i use to draw the table and write the table is the following:

using (StreamWriter file = new StreamWriter("tabela.csv"))
{
using(var csvwriter = new CsvWriter(file,CultureInfo.InvariantCulture))
{
            <table class="table table-bordered table-striped" style="width:95%" >
            <thead>
                <tr>
                    <th width:"30%">
                        Ap_Name
                    @{
                       file.Write("ApName");
                       file.Write(";");
                    }  
                    </th>
                    @{var u = Model.Inicial;
                      var ultimoap = 0;                   
                    }
                    @while( u<=Model.Final)
                    {
                        <th>
                            @u.ToString("MM/yyyy")
                        </th>
                       file.Write(u.ToString("MM/yyyy"));
                       file.Write(";");
                        u =u.AddMonths(1);
                    }

                </tr>
            </thead>
            <tbody>
        
        

            @for(int y = 0; y<Model.Aps.Count; y++)
            {
                file.WriteLine();
                var dadosap = Model.dados2.Where(s => s.ap_id == Model.Aps[y].ap_id).ToList();
                <tr></tr>
                <td style="width:30%">
                    @Model.Aps[y].ap_name
                </td>
                file.Write(Model.Aps[y].ap_name);
                file.Write(";");
                u = Model.Inicial;
                while( u.Date<=Model.Final.Date)
                {
                    var NumAcesso = 0;
                    for(int i = 0; i<dadosap.Count(); i++)
                    {
                        if(dadosap[i].MES == u.Month && dadosap[i].year == u.Year)
                        {
                            NumAcesso = dadosap[i].numeroAcessos;
                        }
                        else
                        {
                           NumAcesso =NumAcesso; 
                        }    
                    }
                        <th> 
                              @NumAcesso
                        </th>
                        file.Write(NumAcesso);
                        file.Write(";");
                    u = u.AddMonths(1);
            }
            }        
                </tbody>
                </table>
}
}

I has thinking on one way to save that file inside a path when i click in the button

<button class="btn btn-primary"  id="ExportToExcel" OnClick="ExportToexcel_Click" value="Export To Excel" ></button>

EDIT:I was thinking and i understand that i was thinking in the wrong way. Now I realized that what i really want, i have the file save in the project folder, and i want that when i click in one button i want to download that file to the client machine.How can i do that?



Solution 1:[1]

After some search i find out that exists a method FileResult that returns a file and just like that i solve my problem.

[HttpPost]
        public FileResult PessoasCOunt(dadosPassar dp)
        {
         StringBuilder sb = new StringBuilder();
         string filename = dp.AcessoVisita + dp.Inicial.ToString() + "_" + dp.Final.ToString() + ".csv";
         return File(System.Text.Encoding.Unicode.GetBytes(sb.ToString()), "text/csv", filename);
        }

And using that i could solve my problem with no problem.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Alexandre Lourenço