下载文件触发beforeunload函数 [英] downloading a file triggers beforeunload function
问题描述
我必须下载pdf文件。当我单击下载按钮时,它会下载文件,但加载图标会一直显示在页面上。虽然页面没有重新加载或加载到浏览器中,但我不明白为什么加载图标显示在页面中。我认为在beforeunloadfunction之前触发了标题功能。因此,加载图标不断显示。如何解决?
ref.php
I have to download a pdf file. When I click a download button it downloads a file but loading icon keeps on displaying on a page. I don't get why loading icon is displaying in the page though a page is not reloading or loading in the browser.I think header function triggered beforeunloadfunction .So,loading icon keeps on displaying. How to resolve it?
ref.php
<?php
$_SESSION['pdf']="example.pdf";
?>
<div id="loading">loading.gif</div>
<button onclick="download_pdf();">download</button>
<script>
$(window).on('beforeunload', function(event) {
$('#loading').show();
});
$(window).on('load', function(event) {
$('#loading').hide();
});
function download_pdf(){
window.location.href="download.php";
}
</script>
download.php
download.php
<?php
session_start();
$pdf=$_SESSION['pdf'];
header("Content-disposition: attachment; filename=$pdf");
header("Content-type: application/pdf");
header('Content-Length: ' . filesize($pdf));
readfile($pdf);
header("Location:ref.php");
?>
当我点击按钮下载文件时我不明白,文件正在下载和
I don't understand when I click the button to download a file, file is getting dowloaded and
$(window).on('beforeunload', function(event) {
$('#loading').show();
});
此函数继续执行,导致完全显示加载图标。
this function keeps on executing, results in showing loading icon alone fully.
推荐答案
_SESSION [' pdf'] = 为例.pdf跨度>;
?>
< div id = loading > loading.gif < / div >
< 按钮 onclick = download_pdf(); > 下载< / button >
< script >
_SESSION['pdf']="example.pdf"; ?> <div id="loading">loading.gif</div> <button onclick="download_pdf();">download</button> <script>
(window).on(' beforeunload ',function( event ){
(window).on('beforeunload', function(event) {
(' #loading')。show();
});
('#loading').show(); });
这篇关于下载文件触发beforeunload函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!