简体   繁体   English

回发后使用C#中已清除的响应执行Javascript

[英]Execute Javascript after postback with cleared response in C#

When a user clicks a button on my website I make an excel file on server-side and write it in the response. 当用户单击我网站上的按钮时,我在服务器端制作了一个excel文件,并将其写入响应中。

HttpResponse httpResponse = Page.Response;
httpResponse.ClearHeaders();
httpResponse.ClearContent();
httpResponse.Clear();
httpResponse.Buffer = true;
httpResponse.AppendHeader("Content-Disposition",
"attachment; " +
"filename=\"" + fileName + "\";");
httpResponse.ContentType = "application/vnd.xlsx";
httpResponse.Charset = "";

using (MemoryStream memoryStream = new MemoryStream())
{
    workbook.SaveAs(memoryStream);
    memoryStream.WriteTo(httpResponse.OutputStream);
    memoryStream.Close();
}

httpResponse.Flush();
httpResponse.End();

When I'm done writing this file, I would like a JavaScript script block to run, but I can't seem to make this work. 编写完该文件后,我希望运行一个JavaScript脚本块,但似乎无法完成这项工作。 I have heard that when I clear the response, I can't write a script block with the Page.ClientScript.RegisterStartupScript (deprecated?), since the user doesn't get the same page as the response, but I am hoping there's a workaround. 我听说当我清除响应时,由于用户没有获得与响应相同的页面,所以无法使用Page.ClientScript.RegisterStartupScript (不建议使用?)编写脚本块,但是我希望有一个解决方法。

The button that makes the postback and fetches the file is not in an UpdatePanel, so I am unable to use the PageRequestManager.add_endRequest function that is implemented there. 进行回发和获取文件的按钮不在UpdatePanel中,因此我无法使用在那里实现的PageRequestManager.add_endRequest函数。 And I can't put the control into an UpdatePanel, since it won't allow me to write a file in an async postback. 而且我无法将控件放入UpdatePanel中,因为它不允许我在异步回发中写入文件。

Is there any other way to trigger a javascript function at the end of a postback? 在回发结束时还有其他方法可以触发javascript函数吗? A JavaScript interval function that checks if a postback is in progress (or finished) is also an acceptable solution as well (but as far as I know, there is no way for JavaScript to "check" if a synchronous postback is in progress). 检查间隔是否正在进行(或已完成)的JavaScript间隔函数也是一种可接受的解决方案(但据我所知,JavaScript无法“检查”同步间隔是否正在进行)。

The Response.End() aborts the thread and sends the execution to Application_EndRequest. Response.End()中止线程并将执行发送到Application_EndRequest。 Nothing can be done after that. 在那之后什么也做不了。 You can either download the file or execute the javascript. 您可以下载文件或执行javascript。 But there is this solution using iframes . 但是有使用iframe的解决方案。

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

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