js使用浏览器的另存为下载文件

页面上的页面如下:

我需要根据返回的url下载文件:

js:

//判断浏览器类型
function myBrowser(){
    var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
    var isOpera = userAgent.indexOf("Opera") > -1;
    if (isOpera) {
        return "Opera"
    }; //判断是否Opera浏览器
    if (userAgent.indexOf("Firefox") > -1) {
        return "FF";
    } //判断是否Firefox浏览器
    if (userAgent.indexOf("Chrome") > -1){
        return "Chrome";
    }
    if (userAgent.indexOf("Safari") > -1) {
        return "Safari";
    } //判断是否Safari浏览器
    if (userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera) {
        return "IE";
    }; //判断是否IE浏览器
    if (userAgent.indexOf("Trident") > -1) {
        return "Edge";
    } //判断是否Edge浏览器
}

 

function SaveAs5(imgURL)
{
    var oPop = window.open(imgURL,"","width=1, height=1, top=5000, left=5000");
    for(; oPop.document.readyState != "complete"; )
    {
        if (oPop.document.readyState == "complete")break;
    }
    oPop.document.execCommand("SaveAs");
    oPop.close();
}

var odownLoad=null;
function downF(obj){
	odownLoad = document.getElementById(obj);
	var url = $(odownLoad).siblings("input[name='fgycompany.fileUrl']").val();
    oDownLoad(url);
}

 

function oDownLoad(url) {
	debugger;
    myBrowser();
    if (myBrowser()==="IE"||myBrowser()==="Edge"){
        //IE
        odownLoad.href="#";
        var oImg=document.createElement("img");
        oImg.src=url;
        oImg.id="downImg";
        var odown=document.getElementById("down");
        odown.appendChild(oImg);
        SaveAs5(document.getElementById('downImg').src)
    }else{
        //!IE
        odownLoad.href=url;
        odownLoad.download="";
    }
}

 

posted @ 2017-04-06 15:33  小安妮的熊  Views(9870)  Comments(0Edit  收藏  举报