<commit_before>﻿using System;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace StackExchange.DataExplorer.Helpers
{
    internal class CsvResult : ActionResult
    {
        private readonly QueryResults results;

        public CsvResult(string json)
        {
            results = QueryResults.FromJson(json);
        }

        public string RawCsv
        {
            get
            {
                var sb = new StringBuilder();
                sb.AppendLine(String.Join(",",
                                          results.ResultSets[0].Columns.Select(col => col.Name).ToArray()));
                sb.Append(String.Join(Environment.NewLine,
                                      results.ResultSets[0].Rows.Select(
                                          row => "\"" + String.Join("\",\"",
                                                             row.ToArray().Select(
                                                                 c => c == null ? "" : c.ToString().Replace("\"", "\"\"")
                                                                 ).ToArray()
                                                     ) + "\""
                                          ).ToArray()
                              ));

                return sb.ToString();
            }
        }

        public override void ExecuteResult(ControllerContext context)
        {
            HttpResponseBase response = context.HttpContext.Response;

            string attachment = "attachment; filename=QueryResults.csv";

            response.Clear();
            response.ContentType = "text/csv";
            response.AddHeader("content-disposition", attachment);
            response.AddHeader("Pragma", "public");
            response.Write(RawCsv);
            response.Flush();
            response.Close();
        }
    }
}<commit_msg>Adding content-length header so Chrome doesn't choke on the download.<commit_after>﻿using System;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace StackExchange.DataExplorer.Helpers
{
    internal class CsvResult : ActionResult
    {
        private readonly QueryResults results;

        public CsvResult(string json)
        {
            results = QueryResults.FromJson(json);
        }

        public string RawCsv
        {
            get
            {
                var sb = new StringBuilder();
                sb.AppendLine(String.Join(",",
                                          results.ResultSets[0].Columns.Select(col => col.Name).ToArray()));
                sb.Append(String.Join(Environment.NewLine,
                                      results.ResultSets[0].Rows.Select(
                                          row => "\"" + String.Join("\",\"",
                                                             row.ToArray().Select(
                                                                 c => c == null ? "" : c.ToString().Replace("\"", "\"\"")
                                                                 ).ToArray()
                                                     ) + "\""
                                          ).ToArray()
                              ));

                return sb.ToString();
            }
        }

        public override void ExecuteResult(ControllerContext context)
        {
            HttpResponseBase response = context.HttpContext.Response;

            response.Clear();
            response.ContentType = "text/csv";
            response.AddHeader("content-disposition", "attachment; filename=QueryResults.csv");
            response.AddHeader("content-length", Encoding.UTF8.GetByteCount(RawCsv).ToString());
            response.AddHeader("Pragma", "public");
            response.Write(RawCsv);
            response.Flush();
            response.Close();
        }
    }
}
{'fraction_non_alphanumeric': 0.1056701030927835, 'fraction_numerical': 0.001288659793814433, 'mean_word_length': 1.12308533916849, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 31, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6810088', 'n_tokens_mistral': 823, 'n_tokens_neox': 801, 'n_words': 150}