guillermorangel
4/3/2017 - 5:13 PM

csv response for csv file download

csv response for csv file download

        public System.Web.HttpResponseBase HistoryExport(long id)
        {
            var quote = _service.Get(id).Quote;
            var historyRecord = quote.Histories.OrderByDescending(x => x.Date);
  
            string csv = string.Empty;
            csv += "Date, User, Action" + "\r\n";
            foreach(var record in historyRecord)
            {
                csv += record.Date.ToString() + ',' + record.User.FirstName + " " + record.User.LastName + ',' + record.Action + "\r\n";
            }

            string csvFileName = "attachment;filename=quoteHistory" + quote.Id + ".csv";
            Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", csvFileName);
            Response.Charset = "";
            Response.ContentType = "application/text";
            Response.Output.Write(csv);
            Response.Flush();
            Response.End();

            return Response;
        }