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 });

 

posted @ 2023-04-15 12:55  若水如引  阅读(243)  评论(0编辑  收藏  举报