c# - Err_Response_Headers_Multiple_Content_Disposition

标签 c# asp.net csv c#-4.0 export-to-csv

我需要单击一次按钮导出 2 个 csv 文件。下面是我生成 2 个 csv 文件的代码:

using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.IO;
using System;
using System.Configuration;
using System.IO.Packaging;
using System.Web;

namespace ExportToCsv
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        //Build the CSV file data as a Comma separated string.
        string csvHeader = string.Empty;
        //Build the CSV file data as a Comma separated string.
        string csvDetails = string.Empty;    

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void ExportCSV(object sender, EventArgs e)
        {
            string constr = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = new SqlCommand("select * from mytable-1")
                {
                    using (SqlDataAdapter sda = new SqlDataAdapter())
                    {
                        cmd.Connection = con;
                        sda.SelectCommand = cmd;
                        using (DataTable dt = new DataTable())
                        {
                            sda.Fill(dt);


                            //foreach (DataRow rows in dt.Rows)
                            //{
                                foreach (DataColumn column in dt.Columns)
                                {
                                    //Add the Header row for CSV file.
                                    csvHeader += column.ColumnName + ' ';
                                }


                                //Add new line.
                                csvHeader += "\r\n";

                                foreach (DataRow row in dt.Rows)
                                {
                                    foreach (DataColumn column in dt.Columns)
                                    {
                                        //Add the Data rows.
                                        csvHeader += row[column.ColumnName].ToString().Replace(",", ";") + ' ';
                                    }

                                    //Add new line.
                                    csvHeader += "\r\n";
                                }
                            //}

                                Response.Clear();
                                Response.Buffer = true;
                                Response.AddHeader("content-disposition", "attachment;filename=HeaderSection.txt");
                                Response.Charset = "";
                                Response.ContentType = "application/text";
                                Response.Output.Write(csvHeader);                                                    
                        }
                    }              
                }

                using (SqlCommand cmd = new SqlCommand("select * from mytable-2")
                {
                    using (SqlDataAdapter sda = new SqlDataAdapter())
                    {
                        cmd.Connection = con;
                        sda.SelectCommand = cmd;
                        using (DataTable dt = new DataTable())
                        {
                            sda.Fill(dt);


                            //foreach (DataRow rows in dt.Rows)
                            //{
                            foreach (DataColumn column in dt.Columns)
                            {
                                //Add the Header row for CSV file.
                                csvDetails += column.ColumnName + ' ';
                            }


                            //Add new line.
                            csvDetails += "\r\n";

                            foreach (DataRow row in dt.Rows)
                            {
                                foreach (DataColumn column in dt.Columns)
                                {
                                    //Add the Data rows.
                                    csvDetails += row[column.ColumnName].ToString().Replace(",", ";") + ' ';
                                }

                                //Add new line.
                                csvDetails += "\r\n";
                            }
                            //}

                            // Download the CSV file.
                            Response.Clear();
                            Response.Buffer = true;
                            Response.AddHeader("content-disposition", "attachment;filename=DetailSection.txt");
                            Response.Charset = "";
                            Response.ContentType = "application/text";
                            Response.Output.Write(csvDetails);
                            Response.Flush();
                            Response.End();
                        }
                    }
                }
            }

        }       
    }
}

我从 2 个不同的表中获取数据并分别导出 csv 中的数据。如果我删除第二个 using 语句,这些函数可以正常工作,但是当我添加第二个 using 语句来编写第二个 csv 时,我的浏览器( Chrome )给出了错误 Err_Response_Headers_Multiple_Content_Disposition
请帮忙。提前致谢。

最佳答案

我用这篇文章解决了

This error was bugging me whole of last week. This error is specific to Google Chrome. Not a bug in chrome itself though, as other browsers ignore duplicate headers, this seems like an issue with Chrome. This can be addressed easily the following ways, if you or your web app is sending out headers.

1 - Enclose filename using "". i.e

header('Content-Disposition: attachment; filename="'.$file_name.'"');

instead of

header('Content-Disposition: attachment; filename='.$file_name);

2 - If possible replace spaces and commas (,) with underscores (_)

$file_name = str_replace(array('"', "'", ' ', ','), '_', $file_name);

3 - Explicitly tell PHP to override headers by setting optional replace parameter to true.

header('Content-type: application/pdf', true);

Fix for Duplicate Headers Error 349

关于c# - Err_Response_Headers_Multiple_Content_Disposition,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39404949/

相关文章:

javascript - 多个下拉框 - 只想从其中获取数据。如何?

c# - Log4net - 删除任何超过 14 天的日志

c# - 如何在azure webjob应用程序中读取azure sql db?

csv - import-csv,get-aduser,然后export-csv筛选出不存在的AD用户

PHP和SQL带来不同的结果

c# - 为什么 MSDN 建议在委托(delegate)声明中包含对象发送者?

c# - 指定 ValidationGroup 时 ValidationSummary 不起作用

asp.net - 无法加载文件或程序集 kre-clr-win - 如何指定 KRE?

javascript - 在 ajax 查询中加载图像 (ASP.NET)

将 CSV 转换为 XML 文件的 Java lib 或应用程序?