Err_Response_Headers_Multiple_Content_Disposition

时间:2016-09-09 06:15:25

标签: 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 )会出现错误{{1} }

请帮忙。提前谢谢。

3 个答案:

答案 0 :(得分:0)

您无法做到这一点,因为HTTP不支持它。您可以做的是将您的文件捆绑到一个文件(例如ZIP文件)中并将其发送给客户端。

答案 1 :(得分:0)

您可以使用 file() 函数来避免手动更改标题:

return response()->file($path);

答案 2 :(得分:-1)

我用这篇文章解决了

  

这个错误让我整个上周都感到烦恼。此错误特定于Google Chrome。虽然不是Chrome本身的错误,因为其他浏览器会忽略重复的标头,这似乎是Chrome的一个问题。如果您或您的网络应用程序正在发送标题,则可以通过以下方式轻松解决此问题。

     

1 - 使用“”括起文件名。即

     

标题('Content-Disposition:attachment; filename =“'。$ file_name。'”');

     

而不是

     

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

     

2 - 如果可能,用下划线(_)

替换空格和逗号(,)      

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

     

3 - 通过将可选的replace参数设置为true,明确告诉PHP覆盖标题。

     

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

     

Fix for Duplicate Headers Error 349