javascript - 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();
解决方案
您可以通过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');
推荐阅读
- spring-boot - 没有找到端点映射 Spring-Boot
- swift - 如何使样式选择器完全可触摸
- javascript - 未捕获的异常:Highcharts 中的范围错误未解决
- javascript - Vue组件没有在项目中呈现,没有任何进一步的错误
- mysql - 如何使用 swift 检查和更新 mySQL?
- r - 将整个 data.table 传递给 j 表达式
- python - 错误:优化器得到一个空的参数列表。如何修改我的代码?
- python - Django模型导入问题
- angular - 从自定义库导入应用程序时,ngrx 商店选择器失败
- reactjs - React:useState 延迟问题