文件下载 方式 web


<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 下载内容
        function href() {
            window.location.href = 'https://*****.oss-cn-**.aliyuncs.com/*********';
            // 兼容模式
            window.location = 'https://*****.oss-cn-**.aliyuncs.com/*********';
        }


        // iframe 方式
        function iframe(url) {
            try {
                var elemIF = document.createElement("iframe");
                elemIF.src = url;
                elemIF.style.display = "none";
                document.body.appendChild(elemIF);
            } catch (e) {
                alert("下载异常!");
            }
        }
        // form 表单
        function downloadFile(url) {
            var form = $("<form>");
            form.attr("style", "display:none");
            form.attr("target", "");
            form.attr("method", "get");
            form.attr("action", url);
            $("body").append(form);
            form.submit();//表单提交}
        }
        // a标签
        function adown(url, name) {
            var a = document.createElement("a");
            a.download = name + ".xls";
            a.href = url;
            $("body").append(a); // 修复firefox中无法触发click
            a.click();
            $(a).remove();
        }
        // 文件流下载
        function formData(url, name) {
            return new Promise((resolve, reject) => {
                var xhr = new XMLHttpRequest();
                xhr.open("POST", url, true); // 也可以使用POST方式,根据接口
                xhr.responseType = "blob"; // 返回类型blob
                // 定义请求完成的处理函数,请求前也可以增加加载框/禁用下载按钮逻辑
                xhr.onload = function () {
                    // 请求完成
                    if (this.status === 200) {
                        // 返回200
                        var blob = this.response;
                        var reader = new FileReader();
                        reader.readAsDataURL(blob); // 转换为base64,可以直接放入a表情href
                        reader.onload = function (e) {
                            // 转换完成,创建一个a标签用于下载
                            var a = document.createElement("a");
                            a.download = name + ".xls";
                            a.href = e.target.result;
                            $("body").append(a); // 修复firefox中无法触发click
                            a.click();
                            resolve(200)
                            $(a).remove();
                        };
                    }
                };
                // 发送ajax请求
                xhr.send(formData);
            })
        };
    </script>
</body>

</html>

转载:https://blog.csdn.net/zhongmushu/article/details/90343986

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

web修理工

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值