Skip to content Skip to sidebar Skip to footer

How Can I Force Download With Html And/or Javascript?

So I have a this code: My Song How can I make it so that instead of the user having to right click, save as, they just cli

Solution 1:

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.


Solution 2:

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.


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


Solution 4:

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


Post a Comment for "How Can I Force Download With Html And/or Javascript?"