[英]C# Postback - set text on page after sending download
我有一個表單,提交后會下載生成的csv文件。 我在頁面上有一個asp:Literal作為占位符,用於加載文本:
<div id="loading-text">
<asp:Literal ID="litLoadingText" runat="server"></asp:Literal>
</div>
$("input[type=submit]").on("click", function() {
//$(".loading-modal").show();
$("#loading-text").html("Loading...");
});
下載完成后,我嘗試更改文本,說“完成!”。 或只是被刪除:
Response.Clear();
Response.Buffer = true;
var fileName = !String.IsNullOrEmpty(txtFileName.Value) ? txtFileName.Value : "ContentExport";
Response.AddHeader("content-disposition", String.Format("attachment;filename={0}.xls", fileName));
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
using (StringWriter sw = new StringWriter())
{
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
litLoadingText.Text = "Done!";
}
但文字沒有改變。 我很確定這是由於下載,我正在執行Response.End()和諸如此類的事情,但是我不確定如何處理它。 是否可以在C#中執行此操作,或者可以使用Javascript方法來檢測何時完成下載? 最好的方法是什么?
問題在這里描述: https : //support.microsoft.com/en-us/help/312629/prb-threadabortexception-occurs-if-you-use-response-end-response-redir
Response.End方法結束頁面執行,並將執行轉移到應用程序的事件管道中的Application_EndRequest事件。 Response.End之后的代碼行未執行。
由於兩個方法都在內部調用Response.End,因此在Response.Redirect和Server.Transfer方法中會發生此問題。
要變通解決此問題,請使用下列方法之一:
對於Response.End,請調用HttpContext.Current.ApplicationInstance.CompleteRequest方法(而不是Response.End),以將代碼執行繞過Application_EndRequest事件。
對於Response.Redirect,請使用重載Response.Redirect(String url,bool endResponse),為endResponse參數傳遞false,以禁止對Response.End的內部調用。 例如:
Response.Redirect ("nextpage.aspx", false);
如果您使用此替代方法,則會執行Response.Redirect之后的代碼。
對於Server.Transfer,請改用Server.Execute方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.