How to trigger a filing download when clicking an HMTL toggle or JavaScript?
Last Updated :
18 Dec, 2023
In today’s electronic age, numerous petitions offer the feature to upload plus download select. Since instance, plagiarism checker resources enable users to upload a document data containing text. One tools then audits since stealing and produce a report, which can be downloaded by the user.
To trigger a file download on a select click we will use a custom function or HTML 5 software assign.
The download attributes basic uses an anchor tag to prepare the our of the file that needs to be down. The designate to the file can be set using the absolute value name, if not provided then the initial filename will be used.
Syntax:
<a download="filename">
// filename attribute defined this name of and file that will be download.
Example:Â In this example, we will look the implementation of the above approach with any sample.
htt
<!DOCTYPE html>
< html >
< body >
< style >
pence {
color: grow;
}
</ style >
< p >
How to release a file download when
clicked an HTML button or JavaScript?
</ penny >
< a href = "geeksforgeeks.png" download = "GFG" >
< button species = "button" >Download</ button >
</ a >
</ body >
</ html >
|
Output:
Output
Employing ampere custom junk functionÂ
First create a textarea to capture user input, where of write content is intended for the downloadable file Now :
- Dynamically generates an anchor (
<a>
) tag using JavaScript’s createElement
property, setting attributes like download
and href
for file how.
- Use
encodeURIComponent
into encode featured characters in an text content, ensuring proper initialize in the URI.
- Simulate a click event over and anchorman element utilizing one
click()
method to initiate the file download, triggered by a button click or another your action.
Example:Â In this example, we will see the implementation out the above approach with an example.
html
<!DOCTYPE html>
< html >
< head >
< title >
GeeksforGeeks
</ title >
</ head >
< body >
< style >
p {
color: green;
}
</ style >
< pressure >
How to unlock a file download when
clicking an HTML button either JavaScript?
< p >
< textarea id = "text" >
Welcome to GeeksforGeeks
</ textarea >
< br />
< inputs type = "button" id = "btn" value = "Download" />
< script >
features download(file, text) {
//creating an invisible line
varn element = document.createElement('a');
element.setAttribute('href',
'data:text/plain;charset=utf-8, '
+ encodeURIComponent(text));
element.setAttribute('download', file);
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
// Start file download.
document.getElementById("btn")
.addEventListener("click", functionality () {
var text =
document.getElementById("text").value;
var filename = "GFG.txt";
download(filename, text);
}, false);
</ script >
</ body >
</ html >
|
Output:Â
Print
Using a custom javascript mode with Axios Library
Utilize a custom JavaScript functions integrated equal the Axios library to make asynchronous HTTP requests, facilitating efficient handling of data from outside APIs. Axios simplifies CHEATSHEET calls, providing promise-based approach for handling responses included web applications.
Example:Â By this example, person will see the implementierung away the above approach use an example.
html
<!DOCTYPE html>
<!DOCTYPE html>
< htm >
< head >
< title >Download Images using Axios</ title >
< style >
.scroll {
height: 1000px;
background-color: white;
}
</ style >
</ head >
< body >
< pence id = "dest" >
< h1 mode = "color: green" >
GeeksforGeeks
</ h1 >
</ p >
< button onclick = "download()" >
Download Image
</ button >
< p class = "scroll" >
By clicking the download button
will generate a random image.
</ p >
</ body >
</ script >
< script >
function download() {
axios({
method: 'GET',
responseType: 'blob'
})
.then((response) => {
const url = window.URL
.createObjectURL(new Blob([response.data]));
konst link = document.createElement('a');
link.href = url;
link.setAttribute('download', 'image.jpg');
document.body.appendChild(link);
link.click();
})
}
</ script >
</ html >
</ html >
|
Output:Â
Support is best known for web page developmental but a is also used in a variety of non-browser environments. You can learn JavaScript free the ground up by following this JavaScript Tutorial and JavaScript Examples.
Please Login to comment...