IE可以实现的浏览器js下载文件的方法

17 篇文章 0 订阅

方法一:

打开新窗口,替换成下载界面

function doSave(obj) {
    obj=document.getElementById('obj');//obj是需要下载的内容
    if (isIE()){//IE浏览器保存文本框内容
    var winname = window.open('', '_blank', 'top=10000');
    winname.document.open('text/html', 'replace');
    winname.document.writeln(obj.value);
    winname.document.execCommand('saveas','','code.htm');//code.htm可改为自定义文件名
    winname.close();}
}

function isIE()//判断浏览器类型
{ 
  if(!!window.ActiveXObject || "ActiveXObject" in window) 
    return true; 
  else 
    return false; 
} 

 

参考:https://www.jb51.net/article/135091.htm?tdsourcetag=s_pctim_aiomsg

 

方法二:

通过blob对象下载

mod.doSave=function(obj,fileName) {
        if (isIE()){//IE浏览器保存文本框内容
            var filename = fileName + ".txt";
            var type = "text/plain; charset=UTF-8";

            var blob = typeof File === 'function'
                ? new File([obj], filename, { type: type })
                : new Blob([obj], { type: type });
            if (typeof window.navigator.msSaveBlob !== 'undefined') {
                // IE workaround for "HTML7007: One or more blob URLs were revoked by closing the blob for which they were created. These URLs will no longer resolve as the data backing the URL has been freed."
                window.navigator.msSaveBlob(blob, filename);
            } else {
                var URL = window.URL || window.webkitURL;
                var downloadUrl = URL.createObjectURL(blob);

                if (filename) {
                    // use HTML5 a[download] attribute to specify filename
                    var a = document.createElement("a");
                    // safari doesn't support this yet
                    if (typeof a.download === 'undefined') {
                        window.location = downloadUrl;
                    } else {
                        a.href = downloadUrl;
                        a.download = filename;
                        document.body.appendChild(a);
                        a.click();
                    }
                } else {
                    window.location = downloadUrl;
                }
            }

        }
    }

方法三:

function downloadFile(url){ 
    //var url = CONTEXTPATH + "/cim/favorite/plotlayer/download?id=" + recordid +     "&filetype=" + filetype; 
    var elemIF = document.createElement("iframe");  
    elemIF.src = url;  
    elemIF.style.display = "none";  
    document.body.appendChild(elemIF);  
 } 

(url是下载的内容)

(此方法同样适用于xls下载,url返回xls表格则下载xls)

参考:https://blog.csdn.net/lishuangzhe7047/article/details/43560447

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值