Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.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#和asp.net创建用于保存csv文件的对话框_C#_Asp.net_Csv_Httpresponse - Fatal编程技术网

使用C#和asp.net创建用于保存csv文件的对话框

使用C#和asp.net创建用于保存csv文件的对话框,c#,asp.net,csv,httpresponse,C#,Asp.net,Csv,Httpresponse,在我的代码中,我有一个asp中继器,我希望允许用户将数据从中导出到csv文件。导出到csv可以很好地工作,但当我希望显示一个对话框供用户选择保存到何处时,不会发生任何情况。我已经经历了几个不同的解决方案,这些解决方案显然是有效的,但当我在代码中运行它们时,什么都没有发生,我也不知道为什么。这是我目前的代码: protected void exportCSV_Click(object sender, EventArgs e) { try { string FileP

在我的代码中,我有一个asp中继器,我希望允许用户将数据从中导出到csv文件。导出到csv可以很好地工作,但当我希望显示一个对话框供用户选择保存到何处时,不会发生任何情况。我已经经历了几个不同的解决方案,这些解决方案显然是有效的,但当我在代码中运行它们时,什么都没有发生,我也不知道为什么。这是我目前的代码:

protected void exportCSV_Click(object sender, EventArgs e)
{
    try
    {
        string FilePath = Server.MapPath("~") + "\\test.csv";
        StringBuilder columnbind = new StringBuilder();
        foreach (Control item in rpt_bookings.Items)
        {
            Literal row1 = (Literal)item.FindControl("ltl_bookingemail");
            Literal row2 = (Literal)item.FindControl("ltl_bookingphone");
            Literal row3 = (Literal)item.FindControl("ltl_bookingcost");
            string fullRow = row1.Text.ToString() + "," + row2.Text.ToString() + "," + row3.Text.ToString();
            columnbind.Append(fullRow);
            columnbind.Append("\r\n");
        }
        //// Creates the file on server
        File.WriteAllText(FilePath, columnbind.ToString());
        string FileName = "test.csv";
        System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
        response.ClearContent();
        response.Clear();
        response.ContentType = "text/csv";
        response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ";");
        response.TransmitFile(FilePath);
        response.Flush();
        response.End();

        //// Deletes the file on server
        File.Delete(FilePath);

        //response.End();
        lblmsg.Text = "";
    }
    catch (Exception ex)
    {
        Console.Write(ex);
        Debug.Write(ex);
        lblmsg.Text = ex.Message;
        lblmsg.Style.Add("color", "#c73939");
    }
}

csv已正确创建,但响应未发生任何变化。代码执行了,但屏幕上什么也没有显示。根据其他几个相同的问题,这就是解决方案。

尝试以下更改:

response.ContentType = "application/octet-stream";
response.AppendHeader("Content-Disposition","attachment; filename=" + Filename + ";");
response.TransmitFile( FilePath );
response.End();
不要删除下一行中的文件,而是删除finally块中的文件:

catch (Exception ex)
{
    [..]
}
finally
{
    File.Delete(FilePath);
}

尝试以下更改:

response.ContentType = "application/octet-stream";
response.AppendHeader("Content-Disposition","attachment; filename=" + Filename + ";");
response.TransmitFile( FilePath );
response.End();
不要删除下一行中的文件,而是删除finally块中的文件:

catch (Exception ex)
{
    [..]
}
finally
{
    File.Delete(FilePath);
}

尝试将转义引号添加到标题的文件名部分<代码>响应.AddHeader(“内容处置”,“附件;文件名=\”“+filename+”\“;”)@DanielPark没有改变任何内容尝试将转义引号添加到标题的文件名部分<代码>响应.AddHeader(“内容处置”,“附件;文件名=\”“+filename+”\“;”)@DanielPark没有改变任何东西