首页 > 解决方案 > Ext.window 中 iframe 的替代品

问题描述

我有一个大的 html 文件,我想通过打开新窗口来下载。我目前正在使用 iframe,它使过程变得漫长、缓慢,并且还阻塞了应用程序。有人可以建议我替代 iframe 吗?请看下面的代码

   var window = new Ext.Window({
                title: "download",
                height: 100,
                layout: 'fit',
                items: [{
                    xtype: 'component',

                    autoEl: {
                            tag: 'iframe',
                            src: 'largedata.html'

                        }
                    }]
            }).show();

标签: javascriptextjs

解决方案


您可以通过XMLHttpRequest隐藏的 dom 元素来做到这一点。

例子:

        var xhr = new XMLHttpRequest();
        xhr.responseType = 'blob';
        xhr.onload = function () {
            if (xhr.status == 200) {
                var name = xhr.getResponseHeader('Content-Disposition');
                var filename = name.split("Attachment;filename=")[1];
                var a = document.createElement('a');
                a.href = window.URL.createObjectURL(xhr.response); // xhr.response is a blob
                a.download = filename; // Set the file name.
                a.style.display = 'none';
                document.body.appendChild(a);
                a.click();
                delete a;
            }
        };
        xhr.open('GET', url, true);
        xhr.send();

参数url是文件的路径。

编辑 2020-04-17:

或者你可以只打开窗口:

window.open(url, '_blank');

推荐阅读