'The load event of an iframe isn't fired while Content-Disposition header is configured as attachment

First of all I just want the load event to be fired only in Mozilla Firefox.

I've already tried different ways, but all I came to know is that load event of an iframe will only gets fired while the 'Content-Disposition' header is configured as 'inline'.

Is there any other event by which i came to know that the iframe dialog box pops up and ask user to save the file?

Please have look into the following implementation of mine and help me to get the proper solution.

HTML code :

<html>
<head>
    <script type="text/javascript" src="FileName.js"></script>
</head>
<body>
    <input type = "button" value="Download" onclick = "javascript:downloadFile('http://localhost/data.php');"></input>
</body>
</html>

JavaScript code :

var downloadFile = function(url) {
// create hidden iframe
var id = "xyz";
var frame = document.createElement('iframe');
frame.id = id;
frame.name = id;
frame.style="visibility:hidden;display:none";
document.body.appendChild(frame);

var callback = function() {
    alert('completed');
};

frame.onload = callback;
frame.src = url;
};

PHP code :

<?php
header('Pragma: public');
header('Expires: 0'); // set expiration time
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Content-Transfer-Encoding: binary');

// Set Content Disposition header
header('Content-Disposition: attachment; filename=some.txt');
// Set content type header
header('Content-Type: application/octet-stream');

$file = "some.txt";

if(!file_exists($file)) die("I'm sorry, the file doesn't seem to exist.");

header('Pragma: no-cache'); 
header('Expires: 0');
readfile($file);
?>

Hoping for a proper solution.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source