How to download file using JavaScript/jQuery?
Hello Archana,
Instead of using jQuery, it’s recommended to use the download
attribute in an <a>
tag for downloading files directly without opening them:
<a href="your_link" download>file_name</a>
This attribute prompts the file to download when the link is clicked.
Dear Archana,
Here is the Answer to the Question,
If you don’t need to navigate to another page, you can use the base JavaScript function window.location.assign('any url or file path')
to trigger a download. This method is useful when you can set the contentType
yourself. Another way to achieve this is by setting window.location.href
to the download file URL. Both methods are straightforward and can be used in any platform where the backend is in JavaScript.