Stop mp3 file from streaming in browsers

前端 未结 1 1264
你的背包
你的背包 2021-02-09 05:27

I have an mp3 on an apache server. I want it to be downloaded as a file when a user visits the link. Instead quicktime, or google chromes media player will try and stream it.

相关标签:
1条回答
  • 2021-02-09 06:33

    You want to set the Content-Disposition response header to "attachment", and probably the "filename" field as well.

    Example:

    Content-Disposition: attachment; filename=foo.mp3;
    

    To accomplish this directly in Apache, try putting the following in your httpd.conf or .htaccess file:

    <FilesMatch "\.(?i:mp3)$">
      ForceType audio/mpeg
      SetEnvIf Request_URI "^.*/?([^/]*)$" FILENAME=$1
      Header set Content-Disposition "attachment; filename=%{FILENAME}e"
      UnsetEnv FILENAME
    </FilesMatch>
    

    EDIT:

    Added filename field to Apache configuration example, borrowed from this answer. You should be aware, however, of potential problems caused by filenames with non-US-ASCII characters in them: see this question.

    Alternatively, you could use a generic constant filename (since you know the extension already) like song.mp3, but this might not be an option depending on your circumstances.

    A third option would be to create a script to serve these MP3s, which can set the header and take care of stripping unwanted characters out of the filename.

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