with just a plain html and js file, how can i force a link to an mp3 to download instead of just play?

前端 未结 7 2053
误落风尘
误落风尘 2021-01-13 15:50

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 min

相关标签:
7条回答
  • 2021-01-13 16:26

    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.net/header

    0 讨论(0)
提交回复
热议问题