Force Download MP3 with PHP

牧云@^-^@ 提交于 2019-12-04 21:42:14

if you are using html5 you can use download option

<a href="url_to_your_file.mp3" download>Download the mp3</a>

otherwise, you can use javascript

function saveAs(url) {    
  var filename = url.substring(url.lastIndexOf("/") + 1).split("?")[0];
  var xhr = new XMLHttpRequest();
  xhr.responseType = 'blob';
  xhr.onload = function() {
    var a = document.createElement('a');
    a.href = window.URL.createObjectURL(xhr.response);
    a.download = filename; 
    a.style.display = 'none';
    document.body.appendChild(a);
    a.click();
    delete a;
  };
  xhr.open('GET', url);
  xhr.send();
}

Call it from your link

<a href="javascript:" onclick="saveAs(url_to_your_file.mp3)">Download the mp3</a>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!