【问题标题】:Is there a polyfill for link download with data uri?是否有用于使用数据 uri 进行链接下载的 polyfill?
【发布时间】:2017-08-03 16:18:48
【问题描述】:

我有一些代码应该由服务器生成:

<a download="test.csv" href="data:text/plain;charset=utf-8;base64,w4FydsOtenTFsXLFkXTDvGvDtnJmw7p0w7Nnw6lwLg==">
    teszt
</a>

它适用于当前的 chrome、firefox、opera。我希望它支持 MSIE11。 Afaik msSaveBlob 是解决方案。是否有我可以使用的现有 js polyfill,或者我应该编写它吗?

【问题讨论】:

    标签: javascript html polyfills


    【解决方案1】:

    好的,我根据在 SO 答案和 here 中找到的代码制作了一个简单的 polyfill。我在 MSIE11 上测试过,它可以工作。它不支持使用XHR 下载文件,只支持数据URI。如果您想强制文件下载,我建议使用Content-Disposition 响应标头。在我的情况下,服务器只是创建文件,但不应该存储它,我也需要一个 HTML 响应,所以这是要走的路。另一种解决方案是通过电子邮件发送文件,但我发现小文件更好。

    (function (){
    
        addEvent(window, "load", function (){
            if (isInternetExplorer())
                polyfillDataUriDownload();
        });
    
        function polyfillDataUriDownload(){
            var links = document.querySelectorAll('a[download], area[download]');
            for (var index = 0, length = links.length; index<length; ++index) {
                (function (link){
                    var dataUri = link.getAttribute("href");
                    var fileName = link.getAttribute("download");
                    if (dataUri.slice(0,5) != "data:")
                        throw new Error("The XHR part is not implemented here.");
                    addEvent(link, "click", function (event){
                        cancelEvent(event);
                        try {
                            var dataBlob = dataUriToBlob(dataUri);
                            forceBlobDownload(dataBlob, fileName);
                        } catch (e) {
                            alert(e)
                        }
                    });
                })(links[index]);
            }
        }
    
        function forceBlobDownload(dataBlob, fileName){
            window.navigator.msSaveBlob(dataBlob, fileName);
        }
    
        function dataUriToBlob(dataUri) {
            if  (!(/base64/).test(dataUri))
                throw new Error("Supports only base64 encoding.");
            var parts = dataUri.split(/[:;,]/),
                type = parts[1],
                binData = atob(parts.pop()),
                mx = binData.length,
                uiArr = new Uint8Array(mx);
            for(var i = 0; i<mx; ++i)
                uiArr[i] = binData.charCodeAt(i);
            return new Blob([uiArr], {type: type});
        }
    
        function addEvent(subject, type, listener){
            if (window.addEventListener)
                subject.addEventListener(type, listener, false);
            else if (window.attachEvent)
                subject.attachEvent("on" + type, listener);
        }
    
        function cancelEvent(event){
            if (event.preventDefault)
                event.preventDefault();
            else
                event.returnValue = false;
        }
    
        function isInternetExplorer(){
            return /*@cc_on!@*/false || !!document.documentMode;
        }
    
    })();
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-22
    • 2021-03-12
    • 2014-09-10
    • 2016-04-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多