明天的明天 永远的永远 未知的一切 我与你一起承担 ??

是非成败转头空 青山依旧在 几度夕阳红 。。。
随笔 - 1277, 文章 - 0, 评论 - 214, 阅读 - 320万
  博客园  :: 首页  :: 管理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

js下载文件 监听下载完成事件

Posted on   且行且思  阅读(2891)  评论(0编辑  收藏  举报
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<br>//下载时加载的操作
    load = function() {
        down_windows = layer.msg("数据正在下载中", { icon: 16, shade: 0.3, time: -1 });
    }
    //下载完成后触发,用来关闭提示框
    disload = function() {
        layer.close(down_windows)
    }
    // 下载使用的方法
    getDownload = function(url) {
        var xhr = new XMLHttpRequest();
        if(typeof(xhr) !="undefined") {
            // 现在使用的方法 可在下载完成的时候关闭load弹窗
            load();
            xhr.open('GET', url, true);    // 也可用POST方式
            xhr.responseType = "blob";
            xhr.onload = function () {
                if (this.status === 200) {
                    var blob = this.response;
                    if (navigator.msSaveBlob == null) {
                        var a = document.createElement('a');
                        var headerName = xhr.getResponseHeader("Content-disposition");
                        a.download = decodeURIComponent(headerName).substring(20);
                        a.href = URL.createObjectURL(blob);
                        $("body").append(a);    // 修复firefox中无法触发click
                        a.click();
                        URL.revokeObjectURL(a.href);
                        $(a).remove();
                    } else {
                        navigator.msSaveBlob(blob, decodeURIComponent(headerName).substring(20));
                    }
                }
                disload();
            };
            xhr.send();
        }
        else {
            window.location.href = url;
        }
    };

 

<script src="xxx/layui/layui.js"></script>
<script src="xxx/jquery.min.js"></script>
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
历史上的今天:
2012-05-24 c# enum 枚举名称和ID 互换获取.................
2012-05-24 Sql : 触发器 update
2006-05-24 DataGrid的多种格式化显示方法
点击右上角即可分享
微信分享提示