admin管理员组文章数量:1345724
i have a link to a media file such as an mp3, and i want it to get downloaded when the user clicks on it, instead of just having the file get played. the page i have in mind is just a plain static html page.
any ideas?
i have a link to a media file such as an mp3, and i want it to get downloaded when the user clicks on it, instead of just having the file get played. the page i have in mind is just a plain static html page.
any ideas?
Share Improve this question asked Dec 3, 2008 at 20:41 derickderick 1,1582 gold badges12 silver badges13 bronze badges7 Answers
Reset to default 5In order to make that happen you need to send a header with Content-disposition: attachment; filename=fname.ext
header in your favorite language before sending the file.
Without knowing the specifics such as what language and what control you have over your server configuration I cannot give you more detailed instructions. You cannot do it with just html and javascript however.
Is far is I know that's not possible. Without the ability to set the appropriate headers the browser will decide what to do with the file, which usually is playback, you will have to ask to users to press, right-click+save as. If you have access to the server it is quite simple to set the headers in php or apache using .htacces
<FilesMatch "\.(?i:mp3)$"> ForceType audio/mpeg Header set Content-Disposition attachment </FilesMatch>
Or so the browser won't recognize it's an MP3 and won't even try opening it:
<FilesMatch "\.(?i:mp3)$"> ForceType application/octet-stream Header set Content-Disposition attachment </FilesMatch>
For php header setting see: http://nl.php/header
<a href="linkto.mp3" download>Download MP3 File </a>
Agree with smazurov, and ultimately you cannot control the presentation of your media although the Content-disposition may get it done for you. Here is the RFC on the header in question.
Client side JavaScript can't achieve that (the nearest functionality is document.execCommand('SaveAs')
, wich just saves the entire html page).
If you want them to be forced to download it from a link, then add the download
attribute to the link element (filename optional):
<a href="file.mp3" download="Song_Name.mp3">Download</a>
If you want users to be forced to download the file when they visit the URL by any means, you are out of luck. That is not possible with just HTML and JS. You have to have access to the server to affect the headers of the file before it gets loaded in the browser, as others have said.
While this is not a solution, change the file extension to anything other than MP3. And, ask the user to rename it once downloaded.
本文标签:
版权声明:本文标题:javascript - with just a plain html and js file, how can i force a link to an mp3 to download instead of just play? - Stack Over 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743814470a2543659.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论