Last active
March 7, 2021 23:03
-
-
Save bennyzhao/6447463 to your computer and use it in GitHub Desktop.
Detect when browser receives file download From:http://stackoverflow.com/questions/1106377/detect-when-browser-receives-file-download
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getCookie( name ) { | |
var parts = document.cookie.split(name + "="); | |
if (parts.length == 2) return parts.pop().split(";").shift(); | |
} | |
function expireCookie( cName ) { | |
document.cookie = | |
encodeURIComponent( cName ) + | |
"=deleted; expires=" + | |
new Date( 0 ).toUTCString(); | |
} | |
function setCursor( docStyle, buttonStyle ) { | |
document.getElementById( "doc" ).style.cursor = docStyle; | |
document.getElementById( "button-id" ).style.cursor = buttonStyle; | |
} | |
function setFormToken() { | |
var downloadToken = new Date().getTime(); | |
document.getElementById( "downloadToken" ).value = downloadToken; | |
return downloadToken; | |
} | |
var downloadTimer; | |
var attempts = 30; | |
// Prevents double-submits by waiting for a cookie from the server. | |
function blockResubmit() { | |
var downloadToken = setFormToken(); | |
setCursor( "wait", "wait" ); | |
downloadTimer = window.setInterval( function() { | |
var token = getCookie( "downloadToken" ); | |
if( (token == downloadToken) || (attempts == 0) ) { | |
unblockSubmit(); | |
} | |
attempts--; | |
}, 1000 ); | |
} | |
function unblockSubmit() { | |
setCursor( "auto", "pointer" ); | |
window.clearInterval( downloadTimer ); | |
expireCookie( "downloadToken" ); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$TOKEN = "downloadToken"; | |
// Sets a cookie so that when the download begins the browser can | |
// unblock the submit button (thus helping to prevent multiple clicks). | |
// The false parameter allows the cookie to be exposed to JavaScript. | |
$this->setCookieToken( $TOKEN, $_GET[ $TOKEN ], false ); | |
$result = $this->sendFile(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function setCookieToken( | |
$cookieName, $cookieValue, $httpOnly = true, $secure = false ) { | |
// See: http://stackoverflow.com/a/1459794/59087 | |
// See: http://shiflett.org/blog/2006/mar/server-name-versus-http-host | |
// See: http://stackoverflow.com/a/3290474/59087 | |
setcookie( | |
$cookieName, | |
$cookieValue, | |
2147483647, // expires January 1, 2038 | |
"/", // your path | |
$_SERVER["HTTP_HOST"], // your domain | |
$secure, // Use true over HTTPS | |
$httpOnly // Set true for $AUTH_COOKIE_NAME | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment