-1

How can I set left mouse click to save the doc (docx) document to my computer? At this moment its only opening doc file in a browser, I want to prevent that. How's that can be done?

<script type="text/javascript">
    function SaveAsMe() {
        e.preventDefault(); // prevent event onclick
        document.execCommand('SaveAs');
    }
</script>

<a href="sites/mydoc.doc" onclick="SaveAsMe()">download doc file</a>

The code above is not working - still opening that doc file in a browser...

I can set that on my test.php page:

header("Content-Type: application/vnd.ms-word; charset=windows-1251");
header("Content-Disposition: attachment; filename=".$node->field_book[0]['filename'].".doc");

but then when I load test.php it always asks me to save that file... How can I make to work that only for links that leading to doc||docx files?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Alexander Kim
  • 17,304
  • 23
  • 100
  • 157

1 Answers1

1

Add the following to your .htaccess file in your sites directory:

<Files *.doc> ForceType application/octet-stream Header set Content-Disposition attachment </Files>

This will force it to download, and you can use a regular <a href=''> to link to the file without any JS intervention.

Kavi Siegel
  • 2,964
  • 2
  • 24
  • 33