Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.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# 将所有当前缓冲区输出发送到客户端并导航到其他页面_C#_Asp.net_Excel - Fatal编程技术网

C# 将所有当前缓冲区输出发送到客户端并导航到其他页面

C# 将所有当前缓冲区输出发送到客户端并导航到其他页面,c#,asp.net,excel,C#,Asp.net,Excel,我想将所有当前缓冲区输出(这是excel工作表中的一些随机代码)发送到客户端并导航到其他页面 我面临着像这样的问题 如果使用Response.End(),页面将停止执行,而Response.Redirect不会执行 如果我使用Response.Flush(),页面将发送缓冲区输出,但在重定向时,出现异常“在发送HTTP头之后无法重定向”。 我的代码如下: // this code sends some codes to excel. ExportToExcel(Codes); Response.

我想将所有当前缓冲区输出(这是excel工作表中的一些随机代码)发送到客户端并导航到其他页面

我面临着像这样的问题

  • 如果使用Response.End(),页面将停止执行,而Response.Redirect不会执行
  • 如果我使用Response.Flush(),页面将发送缓冲区输出,但在重定向时,出现异常“在发送HTTP头之后无法重定向”。
  • 我的代码如下:

    // this code sends some codes to excel.
    ExportToExcel(Codes);
    Response.End(); // what should i use
    Response.Redirect("Coupons.aspx");
    
    ExportToExcel方法:

    public void ExportToExcel(List<string> codes)
    {
        Response.ClearContent();
        Response.Buffer = true;
        Response.AddHeader("content-disposition",
            string.Format("attachment; filename={0}_CODES.xls", DateTime.Today.Date.ToString("d")));
        // Response.AddHeader("content-disposition", @"attachment;filename=""MyFile.pdf""");
        Response.ContentType = "application/excel";
    
        List<string> codeList = codes;
    
        string str = "COUPON CODES";
    
        Response.Write(str);
        str = "\t";
        Response.Write("\n");
        Response.Write("\n");
        foreach (string code in codeList)
        {
            Response.Write(code);
            Response.Write("\n");
        }
    }
    
    public void ExportToExcel(列出代码)
    {
    Response.ClearContent();
    Response.Buffer=true;
    Response.AddHeader(“内容处置”,
    string.Format(“附件;文件名={0}_code.xls”,DateTime.Today.Date.ToString(“d”);
    //Response.AddHeader(“内容处置”,“附件;文件名=”“MyFile.pdf”“”);
    Response.ContentType=“应用程序/excel”;
    列表代码列表=代码;
    string str=“优惠券代码”;
    反应。书写(str);
    str=“\t”;
    响应。写入(“\n”);
    响应。写入(“\n”);
    foreach(代码列表中的字符串代码)
    {
    响应。编写(代码);
    响应。写入(“\n”);
    }
    }
    
    这是我第一次在这里提问。。任何回复和建议都会对我有所帮助


    提前感谢。

    正常的HTTP请求只有一个答案-您已经将响应与Excel工作表一起发送了。您正在尝试发送另一个响应(第二个响应,未定义)。您应该考虑采取另一种方法——在新窗口中打开Excel文档的链接(因为它是下载)关闭,然后单击“导航主窗口”到下一页。例如,您不能同时向客户端发送两个响应。Response.End()发送HTTP 200 OK和excel文件内容,并向调用方抛出ThreadAbortException。另一方面,重定向发送HTTP 302。您不能期望浏览器获取excel文件响应并继续侦听更多响应。将ExportToExcel更改为HttpHandler,从此页面重定向,并在重定向页面上,在新选项卡或新窗口中导航到HttpHandler我知道我无法为单个请求发送2个响应。但我想知道是否有任何方法可以将这两个响应作为单个响应发送。。我只是好奇。对不起,我的英语不好,没有办法。顺便说一句,你的英语还可以。@OguzOzgul谢谢:)我会用另一种方式做的