developer tip

Internet Explorer에서 파일 자동 다운로드를 시작하는 방법은 무엇입니까?

optionbox 2020. 11. 6. 08:05
반응형

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>

"감사합니다"페이지가있는 버전 :

다운로드 후 "감사"를 표시하려면 다음을 사용하십시오.

<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문제의 다운로드를 가리키는 링크에 호출 된 속성을 추가하십시오 .

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

이 모든 작업은 1 초 반을 기다린 다음 다운로드하려는 파일을 참조하는 iframe과 함께 div를 추가하는 것입니다. iframe이 페이지에 업데이트되면 브라우저가 파일을 다운로드합니다. 그렇게 간단합니다. :디


Chrome, firefox 및 IE8 이상에서 작동합니다.

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

이것은 내가 일부 사이트에서 사용하는 것입니다 (jQuery가 필요합니다) .:

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

파일은 1 초 후에 자동으로 다운로드됩니다.


나는 이것을 사용하고 작동하는 것처럼 보이며 프레임 워크가없는 단순한 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>

참고 : 페이지가로드되는 순간 시간 초과가 시작됩니다.


확인하고 찾았습니다. onclick 이벤트를 앵커 태그 또는 입력 버튼에 작성하여 버튼 클릭에서 작동합니다.

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

하나 더 :

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는 사용자가 먼저 저장하지 않고 다운로드를 "열려고"하면 이와 관련된 문제가 있습니다.


뿌리로 돌아가서 나는 이것을 사용합니다.

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

Maybe not WC3 conform but works perfect on all browsers, no HTML5/JQUERY/Javascript.

Greetings Tom :)


This seemed to work for me - across all browsers.

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

I think this will work for you. But visitors are easy if they got something in seconds without spending more time and hence they will also again visit your site. <a href="file.zip" onclick="if (event.button==0) setTimeout(function(){document.body.innerHTML='thanks!'},500)"> Start automatic download! </a>


For those trying to trigger the download using a dynamic link it's tricky to get it working consistently across browsers.

I had trouble in IE10+ downloading a PDF and used @dandavis' download function (https://github.com/rndme/download).

IE10+ needs msSaveBlob.


I hope this will works all the browsers. You can also set the auto download timing.

<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


Nice jquery solution:

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

You can even set different file name for download inside <a> tag:

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

참고URL : https://stackoverflow.com/questions/156686/how-to-start-automatic-download-of-a-file-in-internet-explorer

반응형