zoukankan      html  css  js  c++  java
  • 支持chrome30下载文件

    function downloadX(url ,fileName){							
    		const xhr = new XMLHttpRequest();
    
    		xhr.open('GET', url, true);
    		xhr.responseType = 'blob';
    		xhr.onload = function() {
    			if (xhr.status === 200) {
    				downloadFile(fileName,xhr.response);				
    			}
    		};
    
    		xhr.send();
    }	
    	
    
    function IE_downloadFile(fileName, content) {
    
        var ifr = document.createElement('iframe');
    
        ifr.style.display = 'none';
    
    
        document.body.appendChild(ifr);
        ifr.contentWindow.document.write(content)
    
        //navigator.userAgent.indexOf("MSIE 6.0") > 0 || navigator.userAgent.indexOf("MSIE 7.0") > 0 || 
        if (navigator.userAgent.indexOf("MSIE 8.0") > 0) {
            fileName = fileName + ".txt";
        }
        ifr.contentWindow.document.execCommand('SaveAs', false, fileName);
    
        document.body.removeChild(ifr);
    
    }
    
    function X_downloadFile(fileName, content) {
        var aLink = document.createElement('a');
        var blob = new Blob([content]);
        var evt = new MouseEvent("click", { "view": window, "bubbles": false, "cancelable": false });                
        aLink.download = fileName;
        aLink.href = URL.createObjectURL(blob);
        aLink.dispatchEvent(evt);    
    }
    
    function downloadFile(fileName, content) {
        if (!/Trident|MSIE/.test(navigator.userAgent))
            return X_downloadFile(fileName, content);
        else
            return IE_downloadFile(fileName, content);
    
    }
    

      

    只是为了chrome30的简单版本

    function downloadX(url, fileName) {
        const xhr = new XMLHttpRequest();
    
        xhr.open('GET', url, true);
        xhr.responseType = 'blob';
        xhr.onload = function () {
            if (xhr.status === 200) {
    
                var aLink = document.createElement('a');
                var blob = new Blob([xhr.response]);
                var evt = new MouseEvent("click", { "view": window, "bubbles": false, "cancelable": false });
                aLink.download = fileName;
                aLink.href = URL.createObjectURL(blob);
                aLink.dispatchEvent(evt);
            }
        };
    
        xhr.send();
    }		
    

      

  • 相关阅读:
    洞察僵尸网络的4条关键线索,你知道吗?
    数据即服务(DaaS)的好处和趋势
    AIOT:什么是智联网,它是未来吗?
    渐变略过效果
    页面头部banner动画效果
    小三角
    监测屏幕宽度
    开关效果
    高级轮播
    手机端跳转页面指定楼层
  • 原文地址:https://www.cnblogs.com/coolyylu/p/10081588.html
Copyright © 2011-2022 走看看