In this article, we will discuss using jQuery to download files. This tutorial provides a basic example of using jQuery to download a file from a URL. I briefly explained the response for the jQuery download file. I briefly explained how to download the CSV file with jQuery. Now let’s get into the details.
I’ll demonstrate a quick and simple way to use jQuery to download a file. window.location.href will be used in order to download the response. Let’s now run the HTML file that is 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: