gpt4 book ai didi

javascript - 从网址下载文件?

转载 作者:行者123 更新时间:2023-12-01 00:46:21 25 4
gpt4 key购买 nike

我正在尝试下载文件,但没有成功。我使用此代码,但单击它打开图像,但我不想这样做..我想下载该图像。有什么建议吗?

  toDataURL(url) {
return fetch(url).then((response) => {
return response.blob();
}).then(blob => {
return URL.createObjectURL(blob);
});
}

然后

async download() {
const a = document.createElement("a");
a.href = await toDataURL("https://cdn1.iconfinder.com/data/icons/ninja-things-1/1772/ninja-simple-512.png");
a.download = "myImage.png";
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
}

最佳答案

不是 Angular 方式,而是使用纯js:Fetch API

function download(url, name) {
fetch(url).then((response) => {
return response.blob().then((b) => {
const a = document.createElement("a");
a.setAttribute("download", name);
a.href = URL.createObjectURL(b);
a.click();
});
});
}

download('https://cdn1.iconfinder.com/data/icons/ninja-things-1/1772/ninja-simple-512.png', 'ninja.png')

关于javascript - 从网址下载文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57324014/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com