I would like to know how to download image using javascript. It downloads but shows failed error,
<div>
<button onClick=this.downloadImage(event, "http://example.net/media/image.png")>Download</button>
</div>
downloadImage = (event, image) => {
var link = document.createElement('a');
link.href = image.toDataUrl("image/png", 1.0);
link.download = 'Download.png';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
You can use download API.
According Developer Mozilla's site, try it out:
function onStartedDownload(id) {
console.log(`Started downloading: ${id}`);
}
function onFailed(error) {
console.log(`Download failed: ${error}`);
}
var downloadUrl = "https://example.org/image.png";
var downloading = browser.downloads.download({
url : downloadUrl,
filename : 'my-image-again.png',
conflictAction : 'uniquify'
});
downloading.then(onStartedDownload, onFailed);
Here is more info, https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/downloads/download
Btw, don't forget about CORS and it's limits.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.