尝试下载文件并在一个响应中更改样式

时间:2017-07-06 20:59:48

标签: c# asp.net excel web header

我想在我的程序中做一些动作。当这些操作运行时,我想告诉客户端这些操作正在进行中。并且在此过程结束后,我想向客户端发送此文件,例如Excel文件。我最小化我的项目以准确显示问题所在

现在,这是我的问题:当我点击Button1_Click并尝试将我的标签样式更改回display:none以及<{p>}的addHeader

Response.AddHeader(
    "content-disposition",
    "attachment; filename=" + @"C:\F\f.xlsx"
);

它不能一起工作......当我删除AddHeader这一行时,样式改变了 以及如何解决这个问题?也许有更好的解决方案?

这是我的aspx clint代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
        <asp:UpdatePanel runat="server" ID="UpdatePanel1">
            <Triggers>
                <asp:PostBackTrigger ControlID="Button1" />
            </Triggers>
            <ContentTemplate>
                <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" OnClientClick="ShowProgress()"/>
            </ContentTemplate>
        </asp:UpdatePanel>
        <asp:UpdateProgress ID="UpdateProgress1" style="display:none" runat="server" >
            <ProgressTemplate>
               The report is creating please wait...        
            </ProgressTemplate>
        </asp:UpdateProgress>

    </form>
    <asp:Label runat="server" ID="l1" style="display:none">The report is creating please wait...   </asp:Label>


    <script type="text/javascript">
        document.getElementById('<% Response.Write(l1.ID); %>').style.display = "none";
        document.getElementById('<% Response.Write(UpdateProgress1.ClientID); %>').style.display = "none";
    function ShowProgress()
    {
        document.getElementById('<% Response.Write(l1.ID); %>').style.display = "inline";
        document.getElementById('<% Response.Write(UpdateProgress1.ClientID); %>').style.display = "inline";
    }
    </script>

</body>
</html>

这是来自服务器端的代码

public partial class WebForm1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       ScriptManager.GetCurrent(this.Page).RegisterPostBackControl(Button1);
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        //FindControl(UpdateProgress1.ID).Visible = true;
        Thread.Sleep(2000);
        //FindControl(UpdateProgress1.ID).Visible = false;
        UpdateProgress1.Attributes.Add("style", "display:none");
        l1.Attributes.Add("style", "display:none");
        //l1.Visible = false;
        sendBackToUser();
    }

    private void sendBackToUser()
    {
        FileInfo file = new FileInfo(@"C:\F\f.xlsx");
        if (file.Exists)
        {
            //Response.Clear();
            //Response.ClearHeaders();
            //Response.ClearContent();
            Response.AddHeader("content-disposition", "attachment; filename=" + @"C:\F\f.xlsx");
            //Response.AddHeader("Content-Type", "application/Excel");
            //Response.ContentType = "application/vnd.xlsx";
            //Response.AddHeader("Content-Length", file.Length.ToString());
            //Response.WriteFile(file.FullName);
            //Response.End();
        }
        else
        {
            Response.Write("This file does not exist.");
        }
    }
}

0 个答案:

没有答案