javascript - 使用 Content-Disposition :attachment 在浏览器中重置等待光标

标签 javascript content-disposition mouse-cursor

以下代码运行良好:

<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# 上下文,但问题不是特定于语言或平台的。

最佳答案

问题似乎是没有触发 EndRequest 函数。您是否尝试查看 pageLoading 和 pageLoaded 事件是否正在触发?

我感觉这些事件没有触发,因为没有任何内容发送回页面(附件没有像内联文档那样到达页面)。

我会将文件下载移动到 iframe 中。以下是如何执行此操作的示例:

function InitializeRequest(sender, args) {
    document.body.style.cursor = 'progress';

    var iframe = document.createElement("iframe");
    iframe.src = 'your pdf file';
    iframe.style.display = "none";
    document.body.appendChild(iframe); 
}

然后就可以访问iframe的onload事件了。

编辑:

进一步搜索使我找到了 this page .它似乎可以完成您想要做的所有事情。

关于javascript - 使用 Content-Disposition :attachment 在浏览器中重置等待光标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2010506/

相关文章:

Android 浏览器/webview 错误?内容处置 : attachment; filename ="xyz.txt"

html - 鼠标指针悬停在 TinyMCE 中不可编辑的元素上

javascript - Document.createElement() 与 Document.createTextNode() - Javascript

javascript - 在苹果脚本函数中使用 Javascript 时出错

java - 文件上传中的内容处理

c# - 以编程方式移动鼠标光标

html - 在 CSS 中禁用鼠标指针和链接

javascript - jQuery - Lightgallery.js - 检测幻灯片即将结束

javascript - jQuery 在 JSFiddle 中有效,但在本地 RoR 中无效。类型错误?

Azure Blob 存储 V2 不再提供内容处置 header