簡體   English   中英

如何在 Internet Explorer 中開始自動下載文件?

[英]How to start automatic download of a file in Internet Explorer?

如何在 Internet Explorer 中初始化文件的自動下載?

例如,在下載頁面中,我希望出現下載鏈接和一條消息:“如果下載沒有自動開始......等”。 下載應在頁面加載后不久開始。

在 Firefox 中,這很容易,您只需要在標題中包含一個元標記, <meta http-equiv="Refresh" content="n;url">其中 n 是秒數, url是下載 URL。 這在 Internet Explorer 中不起作用。 如何在 Internet Explorer 瀏覽器中實現此功能?

SourceForge使用<iframe>元素,其中src=""屬性指向要下載的文件。

<iframe width="1" height="1" frameborder="0" src="[File location]"></iframe>

(副作用:沒有重定向,沒有 JavaScript,原始 URL 保持不變。)

討厭網站使下載變得如此復雜並使用黑客而不是一個好的舊鏈接

死簡單版本:

<a href="file.zip">Start automatic download!</a>

有用! 在每個瀏覽器中!


如果您要下載通常內嵌顯示的文件(例如圖像),則 HTML5 具有強制下載文件的download屬性。 它還允許您覆蓋文件名(盡管有更好的方法):

<a href="report-generator.php" download="result.xls">Download</a>

帶有“感謝”頁面的版本:

如果要在下載后顯示“thanks”,請使用:

<a href="file.zip" 
   onclick="if (event.button==0) 
     setTimeout(function(){document.body.innerHTML='thanks!'},500)">
 Start automatic download!
</a>

setTimeout函數可能更高級,例如通過 AJAX 下載完整頁面(但不要離開頁面——不要觸摸window.location或激活其他鏈接)。

關鍵是下載鏈接是真實的,可以復制、拖動、被下載加速器攔截、獲取:visited顏色、如果瀏覽器重啟后頁面保持打開狀態則不會重新下載等。

這就是我用於 ImageOptim 的內容

我最近通過在頁面上放置以下腳本來解決它。

setTimeout(function () { window.location = 'my download url'; }, 5000)

我同意元刷新會更好,但如果它不起作用你怎么辦......

我遇到了類似的問題,上述解決方案都不適合我。 這是我的嘗試(需要 jquery):

$(function() {
  $('a[data-auto-download]').each(function(){
    var $this = $(this);
    setTimeout(function() {
      window.location = $this.attr('href');
    }, 2000);
  });
});

用法:只需在指向相關data-auto-download的鏈接中添加一個名為data-auto-download的屬性:

<p>The download should start shortly. If it doesn't, click
<a data-auto-download href="/your/file/url">here</a>.</p>

它應該適用於所有情況。

一個簡單的 jQuery 為我解決了這個問題。

$(function() {
   $(window).bind('load', function() {
      $("div.downloadProject").delay(1500).append('<iframe width="0" height="0" frameborder="0" src="[YOUR FILE SRC]"></iframe>'); 
   });
});

在我的 HTML 中,我只是有

<div class="downloadProject"></div>

所有這一切都是等待一秒半,然后將 div 與 iframe 附加到您要下載的文件。 當 iframe 更新到頁面上時,您的瀏覽器會下載該文件。 就那么簡單。 :D

適用於 Chrome、Firefox 和 IE8 及更高版本:

var link = document.createElement('a');
document.body.appendChild(link);
link.href = url;
link.click();

我用過這個,似乎有效,只是簡單的 JS,沒有框架:

Your file should start downloading in a few seconds. 
If downloading doesn't start automatically
<a id="downloadLink" href="[link to your file]">click here to get your file</a>.

<script> 
    var downloadTimeout = setTimeout(function () {
        window.location = document.getElementById('downloadLink').href;
    }, 2000);
</script>

注意:這會在頁面加載的那一刻開始超時。

這是我在某些站點中使用的(需要 jQuery)。:

$(document).ready(function() {
    var downloadUrl = "your_file_url";
    setTimeout("window.location.assign('" + downloadUrl + "');", 1000);
});

文件會在 1 秒后自動下載。

我檢查並發現,它可以通過將 onclick 事件寫入錨標記或輸入按鈕來處理按鈕單擊

onclick='javascript:setTimeout(window.location=[File location], 1000);'

回到根源,我用這個:

<meta http-equiv="refresh" content="0; url=YOURFILEURL"/>

也許不符合 WC3,但在所有瀏覽器上都能完美運行,沒有 HTML5/JQUERY/Javascript。

問候湯姆:)

多一個 :

var a = document.createElement('a');
a.setAttribute('href', dataUri);
a.setAttribute('download', filename);

var aj = $(a);
aj.appendTo('body');
aj[0].click();
aj.remove();

確保提供沒有無緩存標頭的文件! 如果用戶嘗試“打開”下載而不先保存,則 IE 會遇到此問題。

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

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

演示在這里

對於那些試圖使用動態鏈接觸發下載的人來說,讓它在瀏覽器中一致工作是很棘手的。

我在 IE10+ 下載 PDF 時遇到了麻煩,並使用了@dandavis 的download功能( https://github.com/rndme/download )。

IE10+ 需要msSaveBlob

這似乎對我有用 - 適用於所有瀏覽器。

 <script type="text/javascript">
    window.onload = function(){
     document.location = 'somefile.zip';
    }
    </script>

我認為這對你有用。 但是,如果訪問者在幾秒鍾內獲得一些東西而無需花費更多時間,那么訪問者很容易,因此他們也會再次訪問您的網站。 <a href="file.zip" onclick="if (event.button==0) setTimeout(function(){document.body.innerHTML='thanks!'},500)"> Start automatic download! </a>

不錯的jQuery解決方案:

jQuery('a.auto-start').get(0).click();

您甚至可以在<a>標簽內設置不同的下載文件名:

Your download should start shortly. If not - you can use
<a href="/attachments-31-3d4c8970.zip" download="attachments-31.zip" class="download auto-start">direct link</a>.
<meta http-equiv="Refresh" content="n;url">

而已。 很簡單,對吧?

 <meta http-equiv="Refresh" content="n;url">

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM