简体   繁体   中英

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. 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? and how to fix that? Maybe have better solution?

this is my aspx clint code :

<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).

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.

As per this answer regarding UpdatePanels and downloads , you need to trigger a full PostBack:

Change your UpdatePanel like this:

<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.

EDIT Below code is in an other project, more simple

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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