Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.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
csv文件未从asp.net c#应用程序在chrome浏览器中下载_C#_Asp.net_Csv - Fatal编程技术网

csv文件未从asp.net c#应用程序在chrome浏览器中下载

csv文件未从asp.net c#应用程序在chrome浏览器中下载,c#,asp.net,csv,C#,Asp.net,Csv,我正在使用以下代码通过http请求发送csv文件。代码运行正常,但该文件未在客户端计算机上下载,未显示“另存为”菜单,未显示任何内容。我在另一个报告中使用了相同的代码,它运行良好,文件也已下载。我正在本地主机上运行该项目。但不适用于按钮BTNGeneratePort protected void btnGenerateReport_Click(object sender, EventArgs e) { DataTable dt = new DataTable();

我正在使用以下代码通过http请求发送csv文件。代码运行正常,但该文件未在客户端计算机上下载,未显示“另存为”菜单,未显示任何内容。我在另一个报告中使用了相同的代码,它运行良好,文件也已下载。我正在本地主机上运行该项目。但不适用于按钮BTNGeneratePort

protected void btnGenerateReport_Click(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        MHSProgressNotesBL oMHSProgressNote = new MHSProgressNotesBL();
        int ClientId = Convert.ToInt32(ddlClients.SelectedValue);
        int LocationId = Convert.ToInt32(ddlLocations.SelectedValue);
        int PractitionerId = Convert.ToInt32(ddlPractitioner.SelectedValue);

        dt = oMHSProgressNote.FetchBillingReport(LocationId, ClientId, PractitionerId, Convert.ToDateTime(txtNoteStartDate.Text), Convert.ToDateTime(txtNoteEndDate.Text));
        WriteToCSV(dt);

    }

    private void WriteToCSV(DataTable dt)
    {
        try
        {
            string attachment = "attachment; filename=ClientRoster.csv";
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ClearHeaders();
            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.AddHeader("content-disposition", attachment);
            HttpContext.Current.Response.ContentType = "text/csv";
            //HttpContext.Current.Response.AddHeader("Pragma", "public");
            WriteColumnName(dt);
            foreach (DataRow clientdata in dt.Rows)
            {
                WriteUserInfo(clientdata);
            }
            HttpContext.Current.Response.End();
            //HttpContext.Current.ApplicationInstance.CompleteRequest();
        }
        catch (Exception e)
        { }

    }

    private void WriteColumnName(DataTable dt)
    {
        string columnNames = "";
        // Write Header row
        foreach (DataColumn item in dt.Columns)
        {
            if (item.ColumnName.Contains(','))
            {
                columnNames += "\"" + item.ColumnName + "\"";
            }
            else
            {
                columnNames += item.ColumnName + ",";
            }
        }
        columnNames = columnNames.TrimEnd(',');

        HttpContext.Current.Response.Write(columnNames);
        HttpContext.Current.Response.Write(Environment.NewLine);
    }

    private void WriteUserInfo(DataRow clientDataRow)
    {
        StringBuilder stringBuilder = new StringBuilder();

        foreach (var item in clientDataRow.ItemArray)
        {
            AddComma((string)(item == System.DBNull.Value ? "" : item.ToString()), stringBuilder);
        }
        HttpContext.Current.Response.Write(stringBuilder.ToString().TrimEnd(','));
        HttpContext.Current.Response.Write(Environment.NewLine);

    }

    private void AddComma(string value, StringBuilder stringBuilder)
    {
        if (value.Contains('"'))
        {
            value = value.Replace(@"""", @"""""");
            value = "\"" + value + "\"";
        }
        else if (value.Contains(','))
        {
            value = "\"" + value + "\"";
        }
        stringBuilder.Append(value);
        stringBuilder.Append(",");
    }

代码没问题。。。。。。。。。主要问题是按钮BTNGeneratePort位于更新面板内。因此出现错误:无法解析从服务器收到的消息。此错误的常见原因是当通过调用response.Write()修改响应、启用响应筛选器、HttpModules或服务器跟踪时。 详细信息:在“客户端,从业者”附近解析时出错。
将按钮放在更新面板外后,代码运行良好

你真的需要重构代码。它的可维护性差,可能会导致问题-您应该有一个生成CSV输出或文件的函数,然后让调用方写入响应,这样所有内容都可以轻松管理,而不是以不同的方法写入响应流。这也会使你更容易针对你已经遇到和将要遇到的问题。是的,没错。无法写入具有“更新”面板的区域。