2012-03-07 108 views
-1

如何設置鼠標左鍵單擊將doc(docx)文檔保存到我的計算機上?在這一刻,它只有在瀏覽器中打開doc文件,我想阻止它。那可以怎麼做?如何用左鍵單擊jQuery或javascript下載doc,docx文件

<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> 

上面的代碼不工作 - 仍然打開一個瀏覽器,doc文件... 在此先感謝。

我可以設置我的test.php頁:

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

但後來當我加載test.php的它總讓我保存該文件...我怎樣才能讓工作只爲導致doc || docx文件的鏈接?

+0

這必須在服務器上進行配置。這是否取決於您使用LAMP的服務器/語言? IIS/.NET? – 2012-03-07 18:57:17

+0

Apache 2.2/PHP – Heihachi 2012-03-07 18:58:16

+0

[強制使用PHP下載文件](http://stackoverflow.com/questions/1465573/forcing-to-download-a-file-using-php) – 2012-03-07 18:59:17

回答

1

以下內容添加到您的.htaccess文件在您的網站目錄:

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

這將迫使它來下載,並可以使用常規的<a href=''>沒有任何干預的JS鏈接到文件。

+0

謝謝,如何設置乘數格式? doc,docx例如? – Heihachi 2012-03-07 19:17:58

+0

只需添加另一行並將'* .doc'更改爲'* .docx'或任何您需要的內容。不幸的是,''指令不允許多個文件! – 2012-03-07 19:52:29

+0

其實,一個修正,用'filesmatch'替換'files'可以讓你做這個'' – 2012-03-07 19:54:09

相關問題