Friday, 1 July 2016

ASP.NET - IE 10 - Fix Error - csv couldn't be downloaded


Watch this example on youtube:

Replace

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < 200000; i++)
            {
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
        
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
                sb.Append("some data here " + Environment.NewLine);
            }
            string sHeader = "attachement; filename=test.csv";
            var exc = HttpContext.Current.Response;
            exc.Clear();
            exc.ContentType = "application/octet-stream";
            exc.ContentEncoding = System.Text.Encoding.Default;
            exc.AppendHeader("Content-Disposition", sHeader);
            exc.Write(sb.ToString());
            exc.Flush();
            exc.Clear();
            exc.Close();


With

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < 200000; i++)
            {
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
        
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
                sb.Append("some data here " + ",");
                sb.Append("some data here " + Environment.NewLine);
            }


            string sHeader = "attachement; filename=test.csv";
            var exc = HttpContext.Current;
            exc.Response.Clear();
            exc.Response.ContentType = "application/octet-stream";
            exc.Response.ContentEncoding = System.Text.Encoding.Default;
            exc.Response.AppendHeader("Content-Disposition", sHeader);
            exc.Response.Write(sb.ToString());
            exc.Response.Flush();
            exc.Response.End();
            exc.ApplicationInstance.CompleteRequest();

1 comment:

  1. Thank you for submitting this. This has solved a big problem for me when exporting in both Excel and PDF formats. I have tried several others online examples but this one worked. I have been telling my users to use Mozilla Firefox as a temporary solution. Thanks again, Grady Christie

    ReplyDelete