Home > Mobile >  I have a file in the project folder and i need when the client click in a button that the file downl
I have a file in the project folder and i need when the client click in a button that the file downl

Time:05-18

I'm developing a school project where I have a file in my project folder, and when i click in a button i need to download that file to the client machine, Im doing the file while writing an html table, i'm using aspnet mvc.

using (StreamWriter file = new StreamWriter("tabela.csv"))
{
using(var csvwriter = new CsvWriter(file,CultureInfo.InvariantCulture))
{
<table  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>

}
}

And when I click in the button, i want to start the download.

<button   id="ExportToExcel"  value="Export To Excel" ></button>

How can i do that??

CodePudding user response:

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.

  • Related