使用 Content-Disposition:attachment 重置浏览器中的等待光标
以下代码运行良好:
<script type="text/javascript">
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_initializeRequest(InitializeRequest);
prm.add_endRequest(EndRequest);
function InitializeRequest(sender, args) {
document.body.style.cursor = 'progress';
}
function EndRequest(sender, args) {
document.body.style.cursor = 'default';
}
</script>
但是,当响应返回附件时(例如,来自 ReportViewer 控件的 PDF):
Response.AddHeader("Content-Disposition", "attachment; filename=some.pdf")
EndRequest() 永远不会触发,并且“进度”光标不会重置。 例如,Javascript 调用
ScriptManager.RegisterStartupScript(this, this.GetType(), "close",
"parent.EndRequest(...);", true);
可以解决这个问题,但由于内容配置的原因,这是不可能的。 您能想出一种方法来正确执行此操作 - 在渲染 PDF 时显示等待光标,并在显示 PDF 时返回到正常光标吗?我在这里假设 C# 上下文,但问题不是特定于语言或平台的。
The following code works nicely:
<script type="text/javascript">
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_initializeRequest(InitializeRequest);
prm.add_endRequest(EndRequest);
function InitializeRequest(sender, args) {
document.body.style.cursor = 'progress';
}
function EndRequest(sender, args) {
document.body.style.cursor = 'default';
}
</script>
When the response returns an attachment, though (a PDF from the ReportViewer control, for instance):
Response.AddHeader("Content-Disposition", "attachment; filename=some.pdf")
EndRequest() never fires and the "progress" cursor is not reset.
A Javascript call, e.g.
ScriptManager.RegisterStartupScript(this, this.GetType(), "close",
"parent.EndRequest(...);", true);
would do the trick, but is not possible because of the content disposition.
Can you think of a way to do this correctly - show a wait cursor while a PDF is rendered, and return to the normal cursor when the PDF is displayed? I am assuming a C# context here, but the problem is not language- or platform-specific.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题似乎是 EndRequest 函数没有被触发。您是否尝试过查看 pageLoading 和 pageLoaded 事件是否正在触发?
我有一种感觉,这些事件没有触发,因为没有任何内容发送回页面(附件不像内联文档那样到达页面)。
我会将文件下载移动到 iframe 中。下面是如何执行此操作的示例:
然后您可以访问 iframe 的 onload 事件。
编辑:
进一步搜索使我到达此页面。它似乎可以完成您想做的所有事情。
The problem seems to be that the EndRequest function is not being fired. Have you tried to see if the pageLoading and the pageLoaded events are firing?
I have a feeling these events are not firing because there is nothing sent back to the page (the attachment doesn't reach the page the same way an inline document does).
I would move the file download into an iframe. Here is an example of how to do that:
Then you can access the onload event of the iframe.
EDIT:
Further searching led me to this page. It seems to do all that you're looking to do.