JS避免下载出现空白页
$sj.tools.DownloadDialog = {
//避免下载出现空白页
openDownloadDialog: function (url, saveName) {
if (typeof url == 'object' && url instanceof Blob) {
url = URL.createObjectURL(url); // 创建blob地址
}
var aLink = document.createElement('a');
aLink.href = url;
aLink.download = saveName || ''; // HTML5新增的属性,指定保存文件名,可以不要后缀,注意,file:///模式下不会生效
var event;
if (window.MouseEvent) event = new MouseEvent('click');
else {
event = document.createEvent('MouseEvents');
event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
}
aLink.dispatchEvent(event);
}
}
$sj.tools.DownloadDialog = {
//避免下载出现空白页
openDownloadDialog: function (url, saveName) {
try{
var elemIF = document.createElement("iframe");
elemIF.src = url;
elemIF.style.display = "none";
document.body.appendChild(elemIF);
}catch(e){
}
}
}
a标签下载出现空白页
今天项目需求,点击下载文件不要跳到空白页
一开始用a标签href文件下载地址测试其他浏览器可以就ie跳到空白页再下载
于是用ifram做中间跳转实现无空白页下载
这样子就可以不需要跳转就可以下载啦
因为直接跳转了所以空白,至于第二次正常是因为浏览器已经知道访问这个地址是下载。
添加download属性
<a href=... download>