javascript 在提交的POST请求完成时收到通知(如果响应不是HTML)

fae0ux8s  于 2023-01-11  发布在  Java
关注(0)|答案(1)|浏览(127)

在我的所有网页表单页面的母版页中,我设置了一个覆盖层,它在表单提交时阻止用户交互,直到浏览器呈现下一页响应:

<script type="text/javascript">
  function overlayWhenSubmit() {
    document.getElementById('spinnerOverlay').style.display = 'block';
  }
  //spinnerOverlay has position:fixed and top/bottom/left/right:0
</script>
protected override void OnPreRender(EventArgs e) {
  this.ClientScript.RegisterOnSubmitStatement(
    typeof(Page), null, "overlayWhenSubmit();"
  );
  base.OnPreRender(e);
}

这工作得很好,直到我尝试提供文件下载:

public static void DownloadFile(this System.Web.HttpResponse @this, string physicalFilePath) {
  @this.ContentType = "application/octet-stream";
  @this.AppendHeader("Content-Disposition", $"Attachment; filename=\"{Path.GetFileName(physicalFilePath)}\"");
  @this.TransmitFile(physicalFilePath);
}
// ... then in some event handler in my page:
Response.DownloadFile(thePathOfFileToDownload);

文件下载正常,但原始页面一直被覆盖,因此无法使用。
有没有一种方法可以在提交的请求完成时通知页面,以便关闭覆盖?

iezvtpos

iezvtpos1#

到目前为止,最接近的方法是尽量不要弹出控件的覆盖层,这会导致非HTML响应:

<script type="text/javascript">
function overlayWhenSubmit() {
  if (!document.getElementById('noOverlayWhenSubmit').value)
    document.getElementById('spinnerOverlay').style.display = 'block';
  document.getElementById('noOverlayWhenSubmit').value = '';
}
function noOverlayWhenSubmit() {
  document.getElementById('noOverlayWhenSubmit').value = true;
}
</script>

<input type="hidden" id="noOverlayWhenSubmit" value="" />

<asp:Button runat="server" OnClientClick="noOverlayWhenSubmit()"></asp:Button>

相关问题