How to Download File using JQuery?

By Hardik Savani November 16, 2022 Category : jQuery

Hi Friends,

In this tute, we will discuss how to download file in jquery. This post will give you a simple example of jquery download file from url. I explained simply about jquery download file response. I explained simply about jquery download csv file. So, let us dive into the details.

I will give you a very simple and short example of how to download file in jquery. we will use window.location.href to download response. so, let's run the below html file.

Example:

index.html

<!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? - ItSolutionStuff.com</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:

I hope it can help you...

Tags :
Shares