Err_Response_Headers_Multiple_Content_Disposition [英] Err_Response_Headers_Multiple_Content_Disposition

查看:2358
本文介绍了Err_Response_Headers_Multiple_Content_Disposition的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

        }       
    }
}

我从2个不同的表中获取数据,并单独导出csv中的数据。如果我删除第二个使用语句的函数工作完成,但因为我添加第二个使用语句写第二个csv我浏览器( Chrome )显示错误 Err_Response_Headers_Multiple_Content_Disposition

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

提前感谢。

推荐答案

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

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.

这篇关于Err_Response_Headers_Multiple_Content_Disposition的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆