开发者

Files downloading involving HTML and JS

开发者 https://www.devze.com 2023-03-07 23:59 出处:网络
Here is the scenario. I have a webpage with files on it. I select two files and they start downloading. All good BUT I want the second file to start downloading automatically only when the first when

Here is the scenario. I have a webpage with files on it. I select two files and they start downloading. All good BUT I want the second file to start downloading automatically only when the first when has finished. This is my code. Can you guys please suggest something?

<h开发者_JAVA技巧tml>
<head>
<script type="text/javascript">
var suffix=1;
function downloadAll(oFrm){
    var oChk = oFrm.elements["file"+(suffix++)];
    if (oChk){
        if (oChk.checked){
            location.href = oChk.value;
            //setTimeout(function(){downloadAll(oFrm)}, 1000);
        }
        else{
            downloadAll(oFrm);
        }
    }
}
</script>
</head>
<body>
<form>
<input type="checkbox" name="file1" id="file1" value="songs/01.mp3" /><label for="file1">1.mp3</label><br />
<input type="checkbox" name="file2" id="file2" value="songs/02.mp3" /><label for="file2">2.mp3</label><br />
<input type="button" value="Download All" onclick="suffix=1;downloadAll(this.form);return false" />
</form>
</body>
</html>


Unfortunately, there isn't a way for JavaScript to tell when the file has finished downloading. The best advice I could suggest is using a back-end server to zip the selected files on request, and then send the ZIP folder to the user.

As for the front-end, you can use jQuery to send the request to the server in realtime.

As for the back-end logic, I would try PHP's Zip extension, which will have to be installed on a server. The server would zip the files requested from jQuery, store the ZIP in a temporary location, and redirect the user to that URL to download.

Hope that helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消