Chris Mccabe
Chris Mccabe

Reputation: 1951

Preventing playing an MP3 file using an HTML anchor

How do you make an MP3 link download instead of play "in-browser"?

I tried changing the target to blank, but that just opened the player in a new window.

Upvotes: 4

Views: 1874

Answers (6)

hobbs
hobbs

Reputation: 239682

For quite some time (Chrome 14+, Firefox 20+, Edge 44+) it is possible to use the download attribute on same-origin links to force download behavior even when the server doesn't supply a Content-Disposition header on the link target.

Upvotes: 0

Blake
Blake

Reputation: 764

I could be wrong, but I've seen people with the same problem before just with other file types, they used the code below:

<FilesMatch "\.(?i:mp3)$">
  ForceType application/octet-stream
  Header set Content-Disposition attachment
</FilesMatch>

Hope this works for you!

Upvotes: 0

Ben
Ben

Reputation: 57209

Lots of solutions here. Here's the basic idea:

  • Use Javascript to make a (ajax) request to the server
  • When the request is received, run a script
  • This script redirects your browser temporarily to a new page
  • The new page has a few headers telling your browser to expect a file attachment, and what kind of attachment it is
  • The "save as" dialog pops up and your original page is still onscreen

Upvotes: 2

Marcus Whybrow
Marcus Whybrow

Reputation: 19988

Without access to the server generating the HTTP responses, the browser gets to decide what to do with different types of responses. usually the only files a browser will download are things like .zip files which it cannot display.

Upvotes: -1

Evan Mulawski
Evan Mulawski

Reputation: 55334

If your server supports PHP, create a PHP script called "getfile.php" (or similar) that takes a parameter of a file ID or file name. Set the content-type and content-disposition headers within the script to force a download prompt.

See: http://webdesign.about.com/od/php/ht/force_download.htm

Upvotes: 3

cdhowie
cdhowie

Reputation: 168958

You can't do this by modifying the link. You will have to have the HTTP server that serves the file send a Content-Type of "application/octet-stream". Presumably it is sending the type "audio/mpeg", which is hinting to the browser that it is MP3 content. Without the capacity to alter this header, you can't achieve this.

Upvotes: 5

Related Questions