js 浏览器中当前页下载文件
1 downloadImg(url:string,name:string):void{ 2 let xhr = new XMLHttpRequest(); 3 xhr.responseType = 'blob'; 4 xhr.open('GET', url); 5 xhr.send(); 6 xhr.addEventListener('load', function() { 7 let blob = xhr.response; 8 let downloadLink = document.createElement('a'); 9 downloadLink.href = URL.createObjectURL(blob); 10 downloadLink.download = name; 11 downloadLink.click(); 12 });