我正在尝试下载文件,但没有成功。我正在使用此代码,但单击它会打开图像,但我不想这样做..我想下载该图像。有什么建议吗?
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);
}
答案 0 :(得分:1)
HTML
<a [href]="imageUrl" download>Download image</a>
component.ts
imageUrl:string = "https://cdn1.iconfinder.com/data/icons/ninja-things-1/1772/ninja-simple-512.png"
答案 1 :(得分:1)
不是有角度的方法,而是使用纯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')
答案 2 :(得分:0)
尝试<a href="data:...." target="_blank">
要么
使用downloadify