0

I'm trying to implement a script on an html template where after loading the template, the file download starts automatically. My files in question are txt, csv and xlsx.

After 2 seconds, as per setTimeout, the script is activated but it does not download the file, but opens a new page as the content of it.

Another question I wanted to ask was whether it is possible to put the absolute path of the file as the folder containing them is nested at a higher level than $ _SERVER ["DOCUMENT_ROOT"].

$file = '/var/www/.........../file.txt';
    echo "<script>
                \$(function() {
                    \$('a[data-auto-download]').each(function(){
                    var \$this = \$(this);
                        setTimeout(function() {
                            window.location.href = \$this.attr('href');
                        }, 2000);
                    });
                });
            </script>
            <a data-auto-download href=\"".$file."\">here</a>";
user3783243
  • 5,368
  • 5
  • 22
  • 41
alby696
  • 1
  • 1
  • [This question](https://stackoverflow.com/questions/23872902/chrome-download-attribute-not-working) might help you to understand, how to force browsers to download files instead of displaying them. – Christopher Jun 22 '22 at 19:55
  • Maybe only ```window.location.href```can be enough for this – Pedro Antônio Jun 23 '22 at 01:00

0 Answers0