coder
coder

Reputation: 13248

Download file using jquery without redirecting

I am trying to download a audio file using jquery as shown below and followed the best answers given but that didn't work out for me in my case.

1.Download file using jquery

2.Using Jquery to download file

$("#downloadbtn").click(function (e) {
    e.preventDefault();  //stop the browser from following
    var downloadUrl = "http://mysite:49223/" + $('#<%=downloadfilename.ClientID %>').val();
    setTimeout("window.location.assign('" + downloadUrl + "');", 1000);
});

And this is my button:

<button id="downloadbtn" class="btn btn-primary downloadbtn">Download</button>

Now the problem is instead of downloading the file its redirecting to the path.

Can anyone say me how do I do this?

Upvotes: 2

Views: 6647

Answers (1)

Shaun
Shaun

Reputation: 1341

Quite an old question, but I was looking to see if there were client side only options. It seems that there is a download attribute as part of HTML 5, but support is fairly restricted at the moment: https://caniuse.com/#feat=download

The other alternative we use is a hidden iframe where you change the Content-Type and/or Content-Disposition headers to get the browser to trigger a download as per this answer: Using jQuery and iFrame to Download a File

Upvotes: 2

Related Questions