views:

937

answers:

6

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?

+5  A: 

In 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.

smazurov
It's also possible to add this statically in IIS if its only a couple of files and you dont want to do it with ASP/PHP.To apply the header statically, right-click the document in the Internet Service Manager, and then select Properties. Select the HTTP Headers tab and enter the header above there.
Stefan
+1  A: 

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

Pim Jager
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.

Jason Jackson
A: 

Client side JavaScript can't achieve that (the nearest functionality is document.execCommand('SaveAs'), wich just saves the entire html page).

mapache
A: 

While this is not a solution, change the file extension to anything other than MP3. And, ask the user to rename it once downloaded.

shahkalpesh
A: 

Using google i found one thread with a js. used to save files. Check it out (posted in 2003, didn't test it myself).

The best soluting would be to use php, asp, or whatever language your server support, to fix this ;-)

qualbeen