Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (13.1k points)
Can anyone help me how I can able to download a file using JavaScript/jQuery? I want to open the download in a new tab. Is it possible to do that?

1 Answer

0 votes
by (26.7k points)

You can able to achieve it, by using iframe:

<iframe id="my_iframe" style="display:none;"></iframe>

<script>

function Download(url) {

    document.getElementById('my_iframe').src = url;

};

</script>

Also, for downloading it in the new window, you need to do it using a link to the target like this using jQuery:

$('a#someID').attr({target: '_blank', 

                    href  : 'http://localhost/directory/file.pdf'});

Whenever that link is clicked, it will download the file in the new tab.

I hope this will help.

Want to become a Java Expert? Join Java Training now!!

Want to know more about Java? Watch this video on Java Tutorial for Beginners | Java Tutorial:

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...