How to prevent the user from doing other things to the page during downloading

  • 2020-03-30 03:28:44
  • OfStack

When making web pages to download files, sometimes the file is too large, it takes a while to generate the file. One way to prevent the user from doing anything else is to use a div to cover the page and lock it.


function lockScreen() 
{ 
sWidth=$(window).width(); 
sHeight=$(window).height(); 
var bgObj=document.createElement("div"); 
bgObj.setAttribute('id','bgDiv'); 
bgObj.style.position="absolute"; 
bgObj.style.top="0"; 
bgObj.style.background="#CCCCCC"; 
bgObj.style.filter="progid:DXImageTransform.Microsoft.Alpha(style=3,opacity=25,finishOpacity=75"; 
bgObj.style.opacity="0.6"; 
bgObj.style.left="0"; 
bgObj.style.width=sWidth + "px"; 
bgObj.style.height=sHeight + "px"; 
if(sWidth < 860) 
{ 
bgObj.style.width="860px"; 
} 
bgObj.style.zIndex = "10000"; 
document.body.appendChild(bgObj); 
}

Use the above function to lock the page to prevent multiple operations, until the download box appears to cancel the lock screen.

Set cookie in server side (cgi) :


<pre name="code" class="cpp">char *configDownloadToken = "finishedDownloadFile"; 
printf("Content-Type: application/octet-streamnContent-Length: %ldn", s.st_size); 
printf( "Set-Cookie:configDownloadToken=%s; path=/; rn ",configDownloadToken); 
printf("Content-Disposition: attachment; filename="%s"n", strrchr(filename,'/') + 1); 
printf("Connection: closenn");

On the client (HTML, js) import plug-in (link: http://xiazai.jb51.net/201406/yuanma/jquery.cookie (jb51.net). Rar), in the HTML file to include this plug-in, js file regularly get a cookie


var configDownloadCheckTimer; 
$(document).ready(function () { 
configDownloadCheckTimer = window.setInterval(function() { 
var cookieValue = $.cookie('configDownloadToken'); 
if (cookieValue === "finishedDownloadFile") 
{ 
refreshPage(); 
finishDownload(); 
} 
}, 1000); 
}); 

function finishDownload() { 
window.clearInterval(configDownloadCheckTimer); 
$.removeCookie('configDownloadToken'); //clears this cookie value 
} 

That's it.


Related articles: