Exemplo n.º 1
0
        public override void ExecuteResult(ControllerContext context)
        {
            var response = context.HttpContext.Response;

            response.Clear();
            response.Buffer          = false;
            response.ContentType     = "text/csv";
            response.ContentEncoding = Encoding.UTF8;
            response.AppendHeader("Content-Disposition", "attachment; filename=\"{0}\"".FormatWith(Filename.Or(DateTime.UtcNow.ToString("_yyyy_MM_dd") + ".csv")));

            _Props.Clear();

            using (var sw = new StreamWriter(response.OutputStream, new System.Text.UTF8Encoding(false), 4096, true))
            {
                // paged query
                int skip = 0;
                int read = _Query.Count();
                while (read > 0)
                {
                    // take a page
                    var page = _Query.Skip(skip).Take(PageSize).ToList();
                    read  = page.Count();
                    skip += read;

                    if (read > 0)
                    {
                        // write csv for the page
                        page.ForEach((row) =>
                        {
                            var map = _SelectMap(row);
                            if (map != null)
                            {
                                if (map is IDictionary <String, object> )
                                {
                                    OutputDictionary(sw, map as IDictionary <String, object>);
                                }
                                else
                                {
                                    OutputAnonymous(sw, map);
                                }
                            }
                        });
                    }
                }
            }

            if (ToDispose != null)
            {
                ToDispose.Dispose();
            }
        }