如何强制在javascript中下载音频文件(mp3)

时间:2018-05-21 09:55:40

标签: javascript download

我正在创建一个系统,当用户点击某个音频的下载链接时,它会将用户重定向到倒计时15秒的下载页面,倒计时后该文件应该下载。
这是我到目前为止所做的:
HTML部分:

<div id="url" hidden>
    http://media.dawahnigeria.com/dnlectures/Shaykh%20M.%20Yahya%20As%20Salafy%20(Kaduna)/Ramadan%201439/Shaykh%20Yahya%20As-Salafy%20-%20Ramadan%201439%20Tafseer%20(Day%2002)%20-%20DN.amr
</div>  

<div id="loader">
    <span id="count">15</span> seconds left to download!<br>
    <progress value="0" max="15" id="progressBar"></progress>
</div><br>

JS部分:

url = document.getElementById('url').innerHTML
function download(url) {
// $("#loader").show();
var timeleft = 15;
var downloadTimer = setInterval(function(){
    document.getElementById("progressBar").value = 15 - --timeleft;
    document.getElementById('count').innerHTML = document.getElementById('count').innerHTML - 1;
    if(timeleft <= 0){
        clearInterval(downloadTimer);
        // console.log('yes');

        var link = document.createElement("a");
        link.download = name;
        link.href = url;
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
        delete link;
        //console.log(url);
    }

},1000);
}
$(window).load(function() {
  download(url);
});  

仅适用于 .amr 文件,但不适用于Google Chrome和Firefox中的 .mp3 文件。
如何使javascript强制下载文件?
提前谢谢。

0 个答案:

没有答案