Download Lister

@using System.IO
@using System.Text

@functions{
   
    public override void InitializePage() {
        
        Response.ContentType = "text/csv";
        Response.Headers.Add("Content-Disposition","attachment;filename=" + Request.Friendly + ".csv");
        base.InitializePage();
      
    }
    
    public string GenerateCSV(string myData) {
        UnicodeEncoding uniEncoding = new UnicodeEncoding();
        
        using(MemoryStream ms = new MemoryStream()){
            var sw = new StreamWriter(ms, uniEncoding);
            sw.Write(myData);
        }
        
        return myData;
    }
    
  
    string People(EngineRecordList item){
        StringBuilder sb = new StringBuilder();

        sb.AppendLine("First Name,Last Name,Email");
        
        foreach(EngineRecord r in item){
            
            sb.Append("\"" + r.GetString("FirstName") + "\"");
            sb.Append(",\"" + r.GetString("LastName") + "\"");
            sb.Append(",\"" + r.GetString("Email") + "\"");
            sb.Append("\n");         
          
        }
        
        return sb.ToString();
    }
 
}   


@if(Request.Friendly == "peopledownload"){


    @BRT.Lister(tableId: "Contacts", viewId: "People",
    condition: "",
    fields: new[] {"FirstName","LastName","Email"},template:
    
         @<text>
                @BRT.Raw(GenerateCSV(People(item)))
         </text>
    ) 
     @RenderBody()
   }else{
    
      <html lang="en">
        <head>
            
        </head>
        <body>
           
            @RenderBody()
       
        </body>
    </html>
  
}