【问题标题】:try to download file from asp.net server and using UpdatePanel, ProgressTemplate.. But not working尝试从 asp.net 服务器下载文件并使用 UpdatePanel、ProgressTemplate .. 但不工作
【发布时间】:2017-12-10 15:17:52
【问题描述】:

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

现在,这是我的问题: Button1 不工作,而 Button2 工作正常,为什么?以及如何解决这个问题? 也许有更好的解决方案?

这是我的 aspx clint 代码:

<form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager>

    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:Button ID="Button1" runat="server" Height="38px" onclick="Button1_Click" Text="Button" Width="167px" />
        </ContentTemplate>
    </asp:UpdatePanel>
    <asp:UpdateProgress ID="UpdateProgress1" runat="server">
        <ProgressTemplate>
            The report is creating please wait...
        </ProgressTemplate>
    </asp:UpdateProgress>

    <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Button" />
</form>

接下来是我的代码

public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {}

        protected void Button1_Click(object sender, EventArgs e)
        {
            Thread.Sleep(5000);
            sendBackToUser();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            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.");
            }
        }
    }

【问题讨论】:

  • 如果您在Button1_Click 事件中设置断点,代码是否到达?
  • 是的。这两者都从头到尾
  • 是 5 秒的等待导致它崩溃吗?你说它不起作用到底发生了什么? (你有错误消息,异常,它只是冻结?)
  • 在 Button1_Click 上它只等待 5 秒,运行整个 sendBackToUser();到达 Button1_Click 的 },完成并没有任何反应但是在 Button2_Click 上,当它结束时,它让我下载 Excel 文件
  • @AntoinePelletier 之前遇到过同样的问题,我认为问题在于 UpdatePanel 只进行了部分回发,因此 OP 将看到发生的事情,从字面上看,什么都没有。看起来按钮根本没有被点击

标签: c# asp.net


【解决方案1】:

虽然 UpdatePanel 是 your previous question 的完美解决方案,但您忽略了提及您也在尝试启动文件下载(您确实在 cmets 中对此有所提及,但我没有清楚地理解您) .

Button1 的问题是因为 UpdatePanel 不会触发页面的完整 PostBack - 它只会回发 UpdatePanel 的内容。

按照this answer regarding UpdatePanels and downloads,你需要触发一个完整的回发:

像这样更改您的 UpdatePanel:

<asp:UpdatePanel runat="server" id="UpdatePanel1">
    <Triggers>
        <asp:PostBackTrigger ControlID="Button1" />
    </Triggers>
    <ContentTemplate>
    ...
    </ContentTemplate>
</asp:UpdatePanel>

【讨论】:

  • UpdatePanel 还是 asp:UpdatePanel?
  • 很好看。它总是&lt;asp:UpdatePanel
  • 首先,非常感谢您抽出宝贵时间关于您所说的话在这种情况下,我看不到“正在创建报告,请稍候...”消息..顺便说一句,我在某个地方看到了我必须添加 ScriptManager.GetCurrent(this.Page).RegisterPostBackControl(Button1);我尝试使用此代码但没有..,在这两种情况下,我都看不到“正在创建报告,请稍候...”消息...
  • Maybe have a look at this。您可能需要异步回发来获得所需的所有功能。你的文件至少下载了吗?
  • 是的,正在下载
【解决方案2】:

这是我项目中的等待框,位于单独的控件中:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WaitingBox.ascx.cs" Inherits="Web_GUI.Controls.WaitingBox" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>

<asp:AlwaysVisibleControlExtender ID="AlwaysVisibleControlExtender1" 
    runat="server" TargetControlID="Panel1" HorizontalSide="Center" 
    VerticalSide="Middle" HorizontalOffset="150">
</asp:AlwaysVisibleControlExtender>

<asp:Panel ID="Panel1" runat="server" CssClass="waitingBox">
    <table>
        <tr>
            <td>Please wait ...</td>
        </tr>    
        <tr>
            <td id="cell_WaitingBox-Panel"><asp:Image ID="imgWaiting" runat="server" ImageUrl="~/Themes/img/loading_animation.gif" /></td>
       </tr>    
    </table>
</asp:Panel>

只需将其添加到使用它的任何其他控件:

        <asp:UpdateProgress ID="UpdateProgress2" runat="server" DisplayAfter="100" AssociatedUpdatePanelID="UpdatePanel1">
            <ProgressTemplate>
                <MyControls:WaitingBox ID="wb2" runat="server" />
            </ProgressTemplate>
        </asp:UpdateProgress>

希望它能启发你...因为我不记得它是如何工作的。

但是一旦它工作了,它可以在你的项目的所有控制中显示一个等待框。

我记得每当后面的代码花费的时间超过 DisplayAfter="100" 时,它会显示一条请稍候消息,可能是 0.1 秒。

EDIT 下面的代码在另一个项目中,更简单

js:

$.ajax({
url: "@Url.Action("create", "Post")",
type: "POST",
contentType: "application/json",
data: JSON.stringify({ model: model})
}).done(function(result){
window.open('@Url.Action("Print", "controler", new { id = Model.id })', '_blank').focus();
}); 

后面的代码:

return Json( new { Whatever...});

但你宁愿尝试调用另一个 javascript 函数,所以......忘记你看到这个

【讨论】:

  • 对于您的实际情况可能有点太复杂了……但以后可以为您服务。
  • 我的按钮是 在你的例子中?要不然是啥?我不明白如何将您的示例转换为我的... % l1.Visible = true; %> 并在 Thread.sleep 之后执行 l1.Visible = false;在点击中
  • 嘿,如果你以我的例子为例,你必须创建一个控制器并将我的第一个代码块(没有标题)粘贴到其中。然后在您的第一个代码块的标题中注册控制器。然后,您所要做的就是将我的第二个代码块放置在控制器可能导致下载的位置,并且该框将自动出现而无需客户端事件。是的,我记得的差不多,如果没有帮助,请见谅。
  • document.getElementById('').style.display = "inline";在这段代码中,我可以在服务器端将其改回 style.display = none 吗?
  • 或者我可以从服务器端运行 Javascript 吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多