简体   繁体   English

尝试从asp.net服务器下载文件,并使用UpdatePanel,ProgressTemplate ..但不起作用

[英]try to download file from asp.net server and using UpdatePanel, ProgressTemplate.. But not working

I Want to make some actions in my program. 我想在程序中做一些动作。 While these actions run I want to tell the client that these actions are on process. 这些动作运行时,我想告诉客户这些动作正在进行中。 and after this process end, I want to send to the client this file for example Excel file. 在此过程结束后,我想将此文件发送给客户端,例如Excel文件。 I minimize my project to show exactly where the problem is 我将项目最小化以准确显示问题所在

Now, this is my problem: Button1 does not work, And Button2 works fine why? 现在,这是我的问题:Button1不起作用,而Button2正常运行,为什么呢? and how to fix that? 以及如何解决? Maybe have better solution? 也许有更好的解决方案?

this is my aspx clint code : 这是我的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>

And next is my code behind 接下来是我的代码

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.");
            }
        }
    }

While the UpdatePanel is a perfectly suitable solution to your previous question , you neglected to mention that you were trying to initiate a file download as well (you did say something about it in comments, but I didn't understand you clearly). 尽管UpdatePanel是您上一个问题的完美解决方案,但您忽略了您也尝试启动文件下载的问题(您确实在评论中说了一些,但我不太清楚)。

The problem with Button1 is because the UpdatePanel doesn't trigger a full PostBack of the page - it only posts back the content of the UpdatePanel. Button1的问题是因为UpdatePanel不会触发页面的完整PostBack-它只会回发UpdatePanel的内容。

As per this answer regarding UpdatePanels and downloads , you need to trigger a full PostBack: 按照有关UpdatePanels和下载的以下答案 ,您需要触发完整的PostBack:

Change your UpdatePanel like this: 像这样更改您的UpdatePanel:

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

Here the waiting box in my project, wich is in a separate control : 这是我项目中的等待框,位于单独的控件中:

<%@ 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>

And simply adding this to any other control that uses it : 并将其简单地添加到其他使用它的控件中:

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

Hope it inspire you... cause i don't exactly remember how it work. 希望它能激发您的灵感……因为我不完全记得它是如何工作的。

But once it work, it can show a waiting box in all the control of your project. 但是一旦工作,它将在项目的所有控件中显示一个等待框。

I remember it shows a please wait message whenever the code behind is taking more than DisplayAfter="100" wich is 0.1 secondes maybe. 我记得当后面的代码花费的时间超过DisplayAfter="100"等于0.1秒时,它会显示一条请稍候的消息。

EDIT Below code is in an other project, more simple 编辑下面的代码在另一个项目中,更简单

js : 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();
}); 

code behind : 后面的代码:

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

But you rather try to call another javascript function so... forget you saw this 但是您宁愿尝试调用另一个javascript函数,所以...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM