132

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?

RedBlueThing
  • 42,006
  • 17
  • 96
  • 122
Dodinas
  • 6,705
  • 22
  • 76
  • 108
  • Possible duplicate of [Download File Using Javascript/jQuery](http://stackoverflow.com/questions/3749231/download-file-using-javascript-jquery) – Liam May 11 '16 at 10:03

10 Answers10

188

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.

karim79
  • 339,989
  • 67
  • 413
  • 406
  • Thanks, this is what I was looking for. I usually use "preventDefault", just left it out above because I was being lazy. ;-) – Dodinas Aug 18 '09 at 21:38
  • 2
    Works great, but get some MIME type errors. Curious if there are any ways to get past them? – Nathan Hangen Dec 31 '12 at 15:42
  • 2
    Did you solve MIME type warning? I get "Resource interpreted as Document but transferred with MIME type..." Many thanks. – user1824269 Jan 16 '13 at 10:19
  • 1
    Im confused. How is this still marked as an answer? Whatever href you put will follow the link. This doesn't work in anyway. – Grogu Apr 17 '22 at 00:38
23

If you don't want search engines to index certain files, you can use robots.txt to tell web spiders not to access certain parts of your website.

If you rely only on javascript, then some users who browse without it won't be able to click your links.

Rob
  • 2,148
  • 13
  • 17
17

Here's a nice article that shows many ways of hiding files from search engines:

http://antezeta.com/news/avoid-search-engine-indexing

JavaScript isn't a good way not to index a page; it won't prevent users from linking directly to your files (and thus revealing it to crawlers), and as Rob mentioned, wouldn't work for all users.
An easy fix is to add the rel="nofollow" attribute, though again, it's not complete without robots.txt.

<a href="uploads/file.doc" rel="nofollow">Download Here</a>
Johny T Koshy
  • 3,857
  • 2
  • 23
  • 40
Kobi
  • 135,331
  • 41
  • 252
  • 292
  • 2
    [Here is another aricle of Google Help for Webmasers](http://www.google.com/support/webmasters/bin/answer.py?hl=de&answer=96569) which helps me a lot to understand the "nofollow" or "me" rel. – 000 Feb 07 '11 at 19:24
13
 var link=document.createElement('a');
 document.body.appendChild(link);
 link.href=url;
 link.click();
EL missaoui habib
  • 1,075
  • 1
  • 14
  • 24
13
  • Using jQuery function

        var valFileDownloadPath = 'http//:'+'your url';
    
       window.open(valFileDownloadPath , '_blank');
    
  • 1
    Hi, this one will open new tab, how if don't want open new tab, just direct download when clicked? already remove `_blank` but no action. – mastersuse Mar 18 '21 at 07:50
12

Yes, you would have to change the window.location.href to the url of the file you would want to download.

window.location.href = 'http://www.com/path/to/file';
MacAnthony
  • 4,471
  • 2
  • 23
  • 26
7

By stating window.location.href = 'uploads/file.doc'; you show where you store your files. You might of course use .htacess to force the required behaviour for stored files, but this might not always be handful....

It is better to create a server side php-file and place this content in it:

header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.$_REQUEST['f']);
readfile('../some_folder/some_subfolder/'.$_REQUEST['f']); 
exit;

This code will return ANY file as a download without showing where you actually store it.

You open this php-file via window.location.href = 'scripts/this_php_file.php?f=downloaded_file';

Daniel
  • 8,655
  • 5
  • 60
  • 87
Otvazhnii
  • 603
  • 5
  • 12
3

I suggest you use the mousedown event, which is called BEFORE the click event. That way, the browser handles the click event naturally, which avoids any code weirdness:

(function ($) {


    // with this solution, the browser handles the download link naturally (tested in chrome and firefox)
    $(document).ready(function () {

        var url = '/private/downloads/myfile123.pdf';
        $("a").on('mousedown', function () {
            $(this).attr("href", url);
        });

    });
})(jQuery);
ling
  • 9,545
  • 4
  • 52
  • 49
3

You can do this with html5 very easily:

var link = document.createElement('a');
link.href = "/WWW/test.pdf";
link.download = "file_" + new Date() + ".pdf";
link.click();
link.remove();
Luca Ziegler
  • 3,236
  • 1
  • 22
  • 39
-1

If somebody comes across this question looking for a way to force downloading an image for example. You could use a download attribute on your <a> tag for that.

<a href="/images/myw3schoolsimage.jpg" download>
Yani
  • 424
  • 6
  • 13