JS通过后台返回的Url下载文件

需求:JS下载后台返回的url

场景:用过window.open和window.location.href,效果不理想,都是打开一个新的网页(碰到txt和png是直接打开,不下载)

 上代码:

function downFile(id,fileName) {
            $.get('Url', { id: id }, function (data) {
                if (data != null && data != '') {
                    let aDom = document.createElement('a');
                    aDom.style.display = 'none';
                    aDom.href = data;
                    aDom.setAttribute('download', fileName);
                    document.body.appendChild(aDom);
                    aDom.click();
                    document.body.removeChild(aDom);
                }
            });
        }

 

感谢:https://www.cnblogs.com/zhangruiqi/p/15936077.html

 

posted @ 2023-02-28 16:36  蜗牛的礼物  阅读(263)  评论(0编辑  收藏  举报