原因
文件下载出现空白页或页面跳转,是因为直接用文件地址跳转window.open或者location.href这种方式会有页面跳转的情况出现。
如果用iframe来做就可以解决这个问题
实例
//传入参数src为文件地址
function download (src) {
var download_file= {}
if (typeof(download_file.iframe) == "undefined") {
var iframe = document.createElement("iframe");
download_file.iframe = iframe;
document.body.appendChild(download_file.iframe);
}
download_file.iframe.src = src
download_file.iframe.style.display = "none";
},
function downloadFile() {
try{
var urlFile="http://localhost/File/test.xls";
var elemIF = document.createElement("iframe");
elemIF.src = urlFile;
elemIF.style.display = "none";
document.body.appendChild(elemIF);
}catch(e){
}
}
本文介绍了一种解决使用window.open或location.href下载文件时出现空白页或页面跳转问题的方法,通过使用iframe元素可以避免这些问题,确保文件下载的顺利进行。

被折叠的 条评论
为什么被折叠?



