[英]try to download file from asp.net server and using UpdatePanel, ProgressTemplate.. But not working
我想在程序中做一些動作。 這些動作運行時,我想告訴客戶這些動作正在進行中。 在此過程結束后,我想將此文件發送給客戶端,例如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.");
}
}
}
盡管UpdatePanel是您上一個問題的完美解決方案,但您忽略了您也嘗試啟動文件下載的問題(您確實在評論中說了一些,但我不太清楚)。
Button1的問題是因為UpdatePanel不會觸發頁面的完整PostBack-它只會回發UpdatePanel的內容。
按照有關UpdatePanels和下載的以下答案 ,您需要觸發完整的PostBack:
像這樣更改您的UpdatePanel:
<asp:UpdatePanel runat="server" id="UpdatePanel1">
<Triggers>
<asp:PostBackTrigger ControlID="Button1" />
</Triggers>
<ContentTemplate>
...
</ContentTemplate>
</asp:UpdatePanel>
這是我項目中的等待框,位於單獨的控件中:
<%@ 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秒時,它會顯示一條請稍候的消息。
編輯下面的代碼在另一個項目中,更簡單
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函數,所以...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.