简体   繁体   中英

Prevent MP3 to Play and Start Downloading

  window.location.href = 'filename.mp3';

I am using javascript to start download MP3 File Download automatically. But it keeps on playing in the browser. Is there any way to start downloading and prevent it to play in browser...

Thanks,

Try This

<script type="text/javascript">

    window.downloadFile = function (sUrl) {
        window.open(sUrl, '_self');
    }
    window.downloadFile("filename.mp3");
</script>

将响应的 MIME 类型设置为 application/octet-stream

Though not really javascript, but what about using the new "download" attribute of the <a> tag?

To automatically trigger such a "click" in javascript, you can use jQuery to build a new DOM anchor obejct with .attr('download', true) and "click" it then. It depends on your actual needs, how to implement that nicely.

edit: remove apache stuff

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM