Err_Response_Headers_Multiple_Content_Disposition
Asked Answered
C

3

12

I have a requirement to export 2 csv files on a single button click. Below is my code to generate 2 csv files:

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();
                        }
                    }
                }
            }

        }       
    }
}

I am getting the data from 2 different tables and export the data in csv seperately. If I remove the second using statement the functions works prefect but as I added the second using statement to write second csv my browser(Chrome) gives the error Err_Response_Headers_Multiple_Content_Disposition

Please help.

Chromous answered 9/9, 2016 at 6:15 Comment(0)
G
30

I solved with this article

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

Gonorrhea answered 9/5, 2017 at 20:16 Comment(2)
Nice one, that worked perfectly for me. Thanks a millionSacculate
This was helpful. Thank you. My code is in ASP.NET and this link provided an example that implements your suggestions. #94051Cusco
S
0

You can use the file() function to avoid manually changing headers:

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

Strouse answered 14/7, 2021 at 15:30 Comment(0)
S
-1

You can't do this because HTTP doesn't support it. What you can do is bundle both your files into a single file (e.g. ZIP file) and send that to the client.

Samanthasamanthia answered 9/9, 2016 at 7:51 Comment(1)
Is it possible to ZIP the files using my posted code. Means I am generating the files on fly and I want to add them in ZIP and then download in the system. I read many articles on ZIP and all have specified file path to add the files in the ZIP. In my case I have no path.Chromous

© 2022 - 2024 — McMap. All rights reserved.