Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start downloading file using JQuery

I want to start file downloading when I clicked the button.
I just have the path of the file.
How can I start downloading?
This is what I tried so far:

$('#download_button').click(function(){

    var filepath = $(this).attr('data-filepath');
    var do = '<a href="'+ filepath +'" download="file">file</a>';
    $(body).append(do);
});

What I am doing wrong.
I never want to redirect the page.
Is downloading start in browser or in software for downloading files if installed on client machine

like image 812
user3316523 Avatar asked Mar 12 '26 09:03

user3316523


1 Answers

Alternatively you can also set the top.location

$('#download_button').click(function(){
    var filepath = $(this).attr('data-filepath');
    top.location.href = filepath;
});
like image 64
Rob Schmuecker Avatar answered Mar 13 '26 21:03

Rob Schmuecker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!