We will talk about how to download files with jQuery in this tutorial. You’ll find a basic example of downloading a file from a URL using jQuery in this post. I gave a brief explanation of the jQuery download file response. I gave a brief explanation of the jQuery download CSV file. Let’s get into the specifics now.
I’ll walk you through a quick and easy example of downloading a file using jQuery. To download the response, window.location.href will be used. Now let’s execute the HTML file below.
Example:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>
<body>
<h1>How to Download File using JQuery?</h1>
<a id="download" href="#">Click Here!</a>
</body>
<script type="text/javascript">
$(document).ready(function () {
$("#download").click(function (e) {
e.preventDefault();
window.location.href = "/docs/demo.csv";
});
});
</script>
</html>
OutPut: