微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

msSaveBlob 文件损坏

如何解决msSaveBlob 文件损坏

我正在尝试使用 JavaScript 在 IE 中下载 .xlsx .pdf .ppt 文件。 我使用了 msSaveBlob,但由于文件损坏,文件没有打开。 我需要你的建议

这是我的代码

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

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

<body>
    <a id='a1' href="http://... .xlsx"
        download>excel</a>
    <script>

        function MS_bindDownload(el) {
            if (el === undefined) {
                throw Error('I need element parameter.');
            }
            if (el.href === '') {
                throw Error('The element has no href value.');
            }
            var filename = el.getAttribute('download');
            console.log("filename : ",filename);
            if (filename === null){
                throw Error('I need download property.');
            }
            if (filename === '') {
                var tmp = el.href.split('/');
                filename = tmp[tmp.length - 1];
            }
            el.addEventListener('click',function (evt) {
                evt.preventDefault();
                var xhr = new XMLHttpRequest();
                xhr.onloadstart = function () {
                    xhr.responseType = 'blob';
                };
                xhr.onload = function () {
                    
                    navigator.msSaveBlob(xhr.response,filename);
                };
                xhr.open("GET",el.href,true);
                xhr.send();
            })
        }
        
        let element = document.querySelector('a');
        MS_bindDownload(element);

    </script>
</body>

</html>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。