Download File Using JQuery or JavaScript




Here we consider how to Download File Using JQuery or JavaScript. For example, Suppose we wants to download a file when you click on a link. For downloading the file, we have two methods. First method is as shown below. Here we would replace the browser URL with the file path.


$('a').click(function(e) {
         var url = "your file url";
          e.preventDefault();
         window.location.href = url;
});



Second method is by assigning href of link with the file path. Let the ID of the link be linkID. When we click on the link, its href attribute will be replaced with the file path. We can open the downloaded file in a separate new tab.


var url = "your file url";
$('#linkID').attr({target: '_blank', href : url});



By using above two methods, we can download file using Jquery.