How to Download Image on Button Click in JavaScript? Last Updated : 31 May, 2024 Comments Improve Suggest changes Like Article Like Report Downloading an image on a button click in JavaScript involves creating a mechanism to trigger the download action when a user clicks a button. Below are the approaches to download image on button click in JavaScript: Table of Content Using Anchor Tag with Download AttributeUsing Fetch APIUsing Anchor Tag with Download AttributeThe "download" attribute in an anchor tag enables direct file downloads from a specified URL when clicked, bypassing the browser's default behaviour of opening the file. It specifies the filename for the downloaded file, simplifying the download process for users and improving overall user experience. Example: In this example a button labelled Download Image, which, upon click, dynamically generates an anchor element to download an image from a specified URL and the image is downloaded with the filename img.jpg. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Image Downloader</title> <style> body { font-family: Arial, sans-serif; display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; } #downloadBtn { padding: 10px 20px; font-size: 16px; background-color: #007bff; color: #fff; border: none; border-radius: 5px; cursor: pointer; transition: background-color 0.3s; } #downloadBtn:hover { background-color: #0056b3; } </style> </head> <body> <button id="downloadBtn">Download Image</button> <script> document .getElementById('downloadBtn') .addEventListener('click', function () { const link = document .createElement('a'); //link to the uploaded image // in your local storage link .href = 'car2.jpg'; link .download = 'img.jpg'; document .body .appendChild(link); link .click(); document .body .removeChild(link); }); </script> </body> </html> Output: Using Fetch APIUsing the Fetch API, an image is fetched asynchronously. The response, typically containing the image data, is converted into a Blob object. This Blob is transformed into a temporary URL. A dynamic anchor element is created with the URL and desired file name. The anchor is then clicked programmatically to start the download and removed for cleanup. Example: To demonstrate creating a "Download Image" button. Clicking it fetches an image from a specified URL via a CORS proxy using JavaScript. The response is converted to a Blob object and downloaded as img.jpg. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Download Image</title> <style> body { font-family: Arial, sans-serif; display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; background-color: #f4f4f4; } #downloadBtn { padding: 10px 20px; font-size: 16px; background-color: #007bff; color: #fff; border: none; border-radius: 5px; cursor: pointer; transition: background-color 0.3s; } #downloadBtn:hover { background-color: #0056b3; } </style> </head> <body> <button id="downloadBtn">Download Image</button> <script> document .getElementById('downloadBtn') .addEventListener('click', function () { const proxyUrl = 'https://cors-anywhere.herokuapp.com/' const targetUrl = 'https://media.geeksforgeeks.org/wp-content/uploads/20240426035114/79dd11a9452a92a1accceec38a45e16a.jpg' fetch(proxyUrl + targetUrl) .then((response) => { console.log('Response:', response) return response.blob() }) .then((blob) => { console.log('Blob:', blob) const url = URL .createObjectURL(blob) const link = document .createElement('a') link .href = url link .download = 'img.jpg' // The name for the downloaded file document .body .appendChild(link) link .click() document .body .removeChild(link) URL.revokeObjectURL(url) }) .catch(console.error) }) </script> </body> </html> Output: Comment More infoAdvertise with us Next Article How to Download Image on Button Click in JavaScript? Anonymous Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads How to Download PDF File on Button Click using JavaScript ? Sometimes, a web page may contain PDF files that can be downloaded by the users for further use. Allowing users to download the PDF files can be accomplished using JavaScript. The below methods can be used to accomplish this task. Table of Content Using html2pdf.js libraryUsing pdfmake LibraryUsing 5 min read How to Download Canvas as Image on Button Click in ChartJS? In Chart.js, the charts that are generated can be converted and downloaded as images for easy sharing and reporting. By using built-in methods like toBase64Image() and the HTML canvas toBlob() method, we can enable users to download charts in formats such as PNG. These techniques provide a simple wa 4 min read How to Display Images in JavaScript ? To display images in JavaScript, we have different approaches. In this article, we are going to learn how to display images in JavaScript. Below are the approaches to display images in JavaScript: Table of Content Using CreateElementUsing InnerHTMLApproach 1: Using CreateElementIn an HTML document, 2 min read How to create an image map in JavaScript ? An image map is nothing but an image that is broken into various hotspots and each hotspot will take you to a different file. Hotspots are nothing but clickable areas which we create on an image by using the <area> tag. This type of map is called a client-side image map as the map is embedded 3 min read How to Trigger a File Download when Clicking an HTML Button or JavaScript? Triggering file downloads in JavaScript refers to initiating the download of files directly from a webpage when a user clicks a button or link. This can be achieved using HTML5's download attribute or custom JavaScript functions, allowing dynamic and user-friendly file-downloading experiences.To tri 4 min read How to Display Image in Alert/Confirm Box in JavaScript ? This article explores how to display an image in an alert box to enhance the user experience with a visually appealing interface. The standard JavaScript alert() method doesn't support images. Table of Content By creating a custom alert box in JavaScriptUsing SweetAlert LibraryBy creating a custom a 3 min read How to Display Image in Alert/Confirm Box in JavaScript ? Using images in alert or confirm boxes in JavaScript adds visual effect and shows the clarity of alert type. With the help of images in alert, messages become more memorable, and accessible, and can attract the user's attention, and the overall user experience is good for use. ApproachAt first, crea 3 min read Create A Download Button with Timer in HTML CSS and JavaScript In this article, we will discuss how to create a Download button with a timer attached to it using HTML, CSS, and Javascript.Our goal is to create a button that has a timer attached to it. The download should only start after the timer has run out, which we will achieve using the setTimeout and setI 2 min read How to change the text and image by just clicking a button in JavaScript ? The image and text can be changed by using JavaScript functions and then calling the functions by clicking a button. We will do that into 3 sections., in the first section we will create the structure by using only HTML in the second section we will design minimally to make it attractive by using si 2 min read How to use document.images in JavaScript ? In JavaScript, the document.images property returns a collection of all "<img>" elements within the current document. We can manipulate these images using various properties and methods. We will see how to work with document.images in JavaScript. ApproachFirst, create a basic HTML structure an 2 min read Like