Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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
<?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
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

JavaScript
$(window).on('beforeunload', function(event) {
   $('#loading').show();    
   }); 

this function keeps on executing, results in showing loading icon alone fully.
Posted
Updated 22-Mar-15 19:55pm
v2
Comments
Mohibur Rashid 22-Mar-15 0:14am    
Does this phP code get executed? You forgot a semicolon after calling the header function.
Vidhya Raju 23-Mar-15 1:57am    
I posted wrongly now I have updated it. Issue is not with it. Actually if I delete beforeunload function den no loading icon appears after downloaded a pdf file. So, issue with beforeunload and header function.
Mohibur Rashid 23-Mar-15 4:35am    
Ok, Because you didn't write a code to hide the #loading

anyway, another issue is i can see a written header after readfile function is called. It would cause you trouble. Move up the header function above readfile.

Remove the last ?> for better practice
Vidhya Raju 23-Mar-15 5:11am    
once page is loaded fully this function $(window).on('load', function(event) {
$('#loading').hide();
}); will hide loading icon fully. Then I tried as u suggested removed ?> and replaced last header line to above readfile function. Now file is not dowloaded instead page is reloading again as header(location:ref.php) is called before readfile function is called. So readfile function doesn't execute .
Mohibur Rashid 23-Mar-15 6:01am    
Rignt, i got it now. Long time ago i answered almost similar question. First of, why your beforeunload triggering: when you are pressing download button, the browser start to load the pdf file in current window-tab. So, current window is being destroyed. That is why onbedoreunload is being triggered. Now what to do: First change your content-type to unknown. It would trigger the browser to download as unknown file. You can save the file. Your beforeunload won't trigger.
Sometime chrome try to act smart. In this case go to google and find out how to disable pdf plugin.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900