한다고 가정 우리는 다음과 같은 HTML 파일이 :iframe에서 파일을 다운로드 할 때 onload 이벤트를 트리거하는 방법은 무엇입니까?
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Test iframe download</title>
<script type="text/javascript">
var init = 0;
function download() {
document.getElementById("dload_frame").src = "http://example.com/dload.py";
}
function alert() {
if (init == 0) {
init = 1;
}
else {
document.getElementById("alert_span").innerHTML = "Got it!";
}
}
</script>
</head>
<body>
<span id="alert_span">Main content.</span><br/>
<input type="button" value="Download" id="btn" onclick="download()" />
<iframe id="dload_frame" src="http://404.com/404" onload="alert()"> </iframe>
</body>
</html>
이제 URL이있는 iframe 대응의 SRC (이 경우 - "http://example.com/dload.py")에 다시 작성되고하는 경우는 HTML 반환, 아무 문제 : onload 이벤트가 발생합니다, 스팬의 내용이 대체되어 모두 행복합니다.
그러나 URL에 의해 반환 된 파일의 콘텐츠 형식이 브라우저가 파일 저장 대화 상자를 강제로 열도록 설정되어 있으면 iframe의 onload 이벤트가 실행되지 않습니다.
해결 방법이 있습니까? iframe을 사용하는 것은 필요하지 않습니다. 브라우저가 제공된 파일을 다운로드하기 시작하면 콜백이 시작됩니다.
아약스를 사용하여 파일을 가져 와서 데이터 URI로 'a' 태그 (링크)를 만들고 클릭하면 큰 파일로 테스트하지 못했습니다. – jcubic