2016-09-22 57 views
0

我試圖編寫代碼打開一個乾淨的頁面,當我打開它時下載一個文件。 我的問題是,我沒有找到如何自動做到這一點。我只覺得這代碼:如何在不點擊按鈕的情況下自動下載文件?

<!DOCTYPE html> 
 
<html> 
 
<body> 
 
<a href="content/file.jpg" download > jpg link </a> 
 
</body> 
 
</html>

回答

2

,不使用後端語言,你可以這個jQuery添加到您的HTML文檔。

<script type="text/javascript"> 
    $(document).ready(function() { 

     $("a").click(); 

    }); 
</script> 

加載文檔後鏈接將自動點擊。

+0

是HTML代碼? – Fox

+1

不,沒有使用像PHP這樣的後端語言,我不知道如何在不模擬按鈕點擊的情況下做你想做的事情。我不相信純HTML您可以指定自動下載 –

+0

我需要添加這在我的.php文件? – Fox

0

我希望這會適用於所有的瀏覽器。您還可以設置自動下載時間。

<html> 
<head> 
<title>Start Auto Download file</title> 
<script src="http://code.jquery.com/jquery-3.2.1.min.js"></script> 
<script> 
$(function() { 
$('a[data-auto-download]').each(function(){ 
var $this = $(this); 
setTimeout(function() { 
window.location = $this.attr('href'); 
}, 2000); 
}); 
}); 
</script> 
</head> 
<body> 
<div class="wrapper"> 
<p>The download should start shortly. If it doesn't, click 
<a data-auto-download href="auto-download.zip">here</a>.</p> 
</div> 
</body> 
</html> 

Demo Here

相關問題