1

So I have a this code:

    <a href="http://mysite.com/song.mp3">My Song</a>

How can I make it so that instead of the user having to right click, save as, they just click the link and BAM it downloads?

saoud
  • 49
  • 1
  • 2
  • 9

4 Answers4

4

On the server side, you can send back a Content-Disposition header, which should work (although its off-spec). See example here:

Uses of content-disposition in an HTTP response header

In future you'll be able to benefit from the new download attribute, part of HTML5.

Community
  • 1
  • 1
isNaN1247
  • 17,793
  • 12
  • 71
  • 118
1

The reason this can't be done without a server-side language is the potential security threats that it poses for users.

You wouldn't like "accidentally" downloading a virus from a site, now would you?

However, as long as the user's browser doesn't support displaying (or playing, in your case) any filetype, it will automatically prompt the user if they want to download the file. So if a browser doesn't offer support for playing mp3 files (I know Chrome does, maybe Firefox), then it should be "automatically downloaded" when clicked.

If you really need another way, though, it can be done with ASP. Here is a tutorial.

Purag
  • 16,941
  • 4
  • 54
  • 75
-3
<a type="application/mpeg" href="song.mp3">song link</a>

This will work on every Browser today, except on mobile dispositives. I would like a method with Javascript or simple html to perform this although. I've tried the meta content-disposition and still don't work.

MRP
  • 9
-3

Audio files by default gets played by audio player/plug-in.
You should put the files in a ".zip", and write the code to download it:

<a href="http://mysite.com/song.zip">My Song</a>

That instantly sets the default action to force download.

Marcela Rocha
  • 79
  • 2
  • 12
  • 1
    This is not completely correct, in some actual browsers the mp3 is played inside the browser, there's not nedded any audio player nor plugin. And soaud tries to make a downloadable link for his songs, creating a zip file is a horrible way to do this. – Andrés Morales Mar 11 '14 at 14:02