12345678910111213141516171819202122232425 |
- export function downloadImage(url, filename) {
- // 创建XMLHttpRequest对象
- var xhr = new XMLHttpRequest();
- xhr.open('GET', url, true);
- xhr.responseType = 'blob';
- // 读取图片数据并创建Blob
- xhr.onload = function() {
- if (xhr.status === 200) {
- var blob = new Blob([xhr.response], { type: 'image/jpeg' });
- // 创建URL对象并模拟点击下载链接
- var url = window.URL.createObjectURL(blob);
- var a = document.createElement('a');
- a.href = url;
- a.download = filename;
- document.body.appendChild(a);
- a.click();
- document.body.removeChild(a);
- }
- };
- // 发送请求
- xhr.send();
- }
|