需求: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
标签:aDom,Url,JS,后台,document,data,id From: https://www.cnblogs.com/dzw159/p/17164951.html