Hello wizards, I hope you are doing great. Today in this blog you’ll learn how to create the "File Downloader" project using HTML, CSS & JavaScript.
Preview
In the above video, you’ve seen the preview of the "File Downloader" project and I hope now you are able to create this type of project. If not, I have provided all the HTML CSS and JavaScript code below.
File Downloader [Source Code]
To get the following HTML, CSS & JS code for the File Downloader project. You need to create three files one is a HTML file, second one is a CSS file and the another one is JS file. After creating these three files then you can copy-paste the given codes on your document.
Remember, you’ve to create a file with .html extension for HTML code, .css extension for CSS code and .js for JavaScript code.
You can also download all source code files from the given download button.
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>File Downloader</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="container">
        <div>
            <h1>File Downloader</h1>
            <p>Download any image, video, or pdf as file by just pasting the URL</p>
        </div>
        <form action="#">
            <input type="url" placeholder="Paste the URL here..." class="input" required>
            <button class="downloadBtn">Download</button>
        </form>
    </div>
    <script src="index.js"></script>
</body>
</html>
    @import url("https://fonts.googleapis.com/css2?family=Poppins:wght@100;200;300;400;500;600;700;800;900&display=swap");
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: "Poppins", sans-serif;
}
body {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  background-color: slateblue;
}
.container {
  width: 400px;
  height: 240px;
  display: grid;
  place-content: center;
  text-align: center;
  background-color: white;
  color: slateblue;
  border-radius: 10px;
  gap: 30px;
  padding: 10px;
}
form {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
}
.input {
  margin-top: -10px;
  width: 80%;
  height: 35px;
  outline: none;
  border: 2px solid slateblue;
  padding: 0 20px;
  border-radius: 10px;
}
.input::placeholder {
  color: gray;
  opacity: 0.8;
}
.input:focus {
  box-shadow: 4px 4px 6px rgba(0, 0, 0, 0.2);
}
.downloadBtn {
  width: 130px;
  height: 30px;
  margin-top: 10px;
  cursor: pointer;
  pointer-events: none;
  background-color: slateblue;
  border: none;
  color: white;
  border-radius: 10px;
  opacity: 0.5;
}
.downloadBtn:hover {
  filter: brightness(1.2);
}
.input:valid ~ .downloadBtn {
  opacity: 1;
  pointer-events: auto;
}
    const inputEl = document.querySelector(".input");
const downloadBtnEl = document.querySelector(".downloadBtn");
downloadBtnEl.addEventListener("click", (event) => {
    event.preventDefault();
    downloadBtnEl.innerText = "Downloading...";
    fetchURL(inputEl.value);
});
async function fetchURL(url) {
    try {
        const data = await fetch(url)
        const blob = await data.blob()
        const fileUrl = URL.createObjectURL(blob)
        const aTagEl = document.createElement("a");
        aTagEl.href = fileUrl;
        // your file is download as "YourFile"
        aTagEl.download = "YourFile";
        // your file is  downloaded as the original name
        // aTag.download = url.replace(/^.*[\\\/]/, '');
        document.body.appendChild(aTagEl);
        aTagEl.click();
        downloadBtnEl.innerText = "Download";
        URL.revokeObjectURL(fileUrl);
        aTagEl.remove();
    } catch {
        alert("Failed to download...!");
        downloadBtnEl.innerText = "Download";
    }
}
     
