Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 错误\u响应\u标题\u多个\u内容\u处置_C#_Asp.net_Csv_C# 4.0_Export To Csv - Fatal编程技术网

C# 错误\u响应\u标题\u多个\u内容\u处置

C# 错误\u响应\u标题\u多个\u内容\u处置,c#,asp.net,csv,c#-4.0,export-to-csv,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 pa

我要求只需单击一个按钮即可导出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();
                        }
                    }
                }
            }

        }       
    }
}
我从两个不同的表中获取数据,并分别以csv格式导出数据。如果我使用语句删除了第二个
,功能会运行得很好,但当我添加第二个
语句时,使用
语句写入第二个csv,我的浏览器(Chrome)会给出错误
错误响应\u标题\u多内容\u配置


请帮忙。提前感谢。

您不能这样做,因为HTTP不支持它。您可以将两个文件捆绑成一个文件(例如ZIP文件)并发送给客户端。

我通过本文解决了这个问题

这个错误上个星期一直困扰着我。此错误特定于Google Chrome。但chrome本身并没有缺陷,因为其他浏览器忽略了重复的标题,这似乎是chrome的一个问题。如果您或您的web应用程序正在发送标题,则可以通过以下方式轻松解决此问题

1-使用“”括起文件名。i、 e

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

而不是

标题('Content-Disposition:attachment;filename='。$file\u name)

2-如有可能,将空格和逗号(,)替换为下划线(33;)

$file_name=str_replace(数组(“,“,”,“,”,”),“,”,$file_name)

3-通过将可选的replace参数设置为true,显式告诉PHP重写标题

标题('Content-type:application/pdf',true)


是否可以使用我发布的代码来压缩文件。这意味着我正在动态生成文件,我想将它们添加到ZIP中,然后在系统中下载。我阅读了许多关于ZIP的文章,所有这些文章都指定了将文件添加到ZIP中的文件路径。在我的情况下,我没有路径。很好,这对我来说非常有效。非常感谢