浏览器下载文件 自带弹框

//ajax中success方法 
success: function (resp) {//resp为文件路径
                var filename = resp.substr(resp.lastIndexOf('\\') + 1);
                var doc = resp.match(/([^\\]+)(\.[^\(]+)/i);
                var h5Down = !/Trident|MSIE/.test(navigator.userAgent); //浏览器是否支持download
                if (h5Down) {
                    openDownloadDialog("http://" + window.location.host + "/Excel/" + filename, filename);
                } else {
                    window.open("http://" + window.location.host + "/Excel/" + filename);
                }
            }
//下载文件
 function openDownloadDialog(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 || '';
        var event;
        event = document.createEvent('MouseEvents');
        event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
        aLink.dispatchEvent(event);
    }

猜你喜欢

转载自blog.csdn.net/qq_34059765/article/details/85319604