jqueryfiledownload

Download File Using jQuery


How can I prompt a download for a user when they click a link.

For example, instead of:

<a href="uploads/file.doc">Download Here</a>

I could use:

<a href="#">Download Here</a>

 $('a').click... //Some jquery to download the file

This way, Google does not index my HREF's and private files.

Can this be done with jQuery, if so, how? Or should this be done with PHP or something instead?


Solution

  • I might suggest this, as a more gracefully degrading solution, using preventDefault:

    $('a').click(function(e) {
        e.preventDefault();  //stop the browser from following
        window.location.href = 'uploads/file.doc';
    });
    
    <a href="no-script.html">Download now!</a>
    

    Even if there's no Javascript, at least this way the user will get some feedback.