Reputation: 4462
I have some MP3 files in a playlist each of which has a link next to it in the form:
<a href="/music/track1.mp3"></a>
The user can right click on the link and choose 'download linked file' (or equivalent option in browser).
What I would like is for files to download automatically when users click on the links (i.e. a standard left click) and not via the "Save As" dialogue, but to the users default download folder for their browser.
What would be the simplest way of achieving this?
Upvotes: 1
Views: 3337
Reputation: 21
If you want a mp3-file being downloaded instead of played in the same or another window, just add a "download" attribute to the link e.g.
<a href="www.mysite.com/uploads/mysong.mp3" download>download my song</a>
or
<a href="www.mysite.com/uploads/mysong.mp3" download="filename.mp3">download my song</a>
The "download" attribute is new for the "a" tag in HTML5, and it's supported by recent browsers. It works for other file-types too and is not restricted to mp3-files.
See: w3 schools page on download attribute
Upvotes: 2
Reputation: 1589
You need to have control over the request to do this. Depending on your Web server technology there are various ways to accomplish. What you want to do is set the 'Content-Disposition' HTTP header to 'attachment; file=somefile'. This tells the browser to download this file instead of 'open' it (if it can). An example in something like c# would be:
response.AddHeader("Content-Disposition", "attachment; filename=downloadThisAudio.mp3"));
But you can accomplish this through configuration in both IIS and Apache. A bit of googling around I think you can find out how to set this headers for MP3's only, or even conditionally (like per directory, or when a query parameter is present).
Upvotes: 0
Reputation: 4830
Using a server side language you can set a Content-Disposition
header to force a download when the user clicks a link, however you do not have control over the browser and where the file is stored on the users machine, this is dependant on the individual users settings.
Upvotes: 1
Reputation: 3315
This will raise many red flags with all the recent browsers.
File downloads are managed via the save as functionality to garantee security when downloading files.
If you could download onclick to a folder what's to stop you to download without a click?
If however you still want to achieve this you would have to use a 3th party component like flash or activeX but these will first ask the user to grant you priviliges(trust).
Upvotes: 0
Reputation: 816
You need to configure your webserver to add Content-Disposition attachment
to the HTTP header. Here are instructions on how to do that with Apache.
As Mitch Satchwell said, you can also do this with server-side languages, that operate on a low-enough level so you can modify the HTTP header.
Upvotes: 1
Reputation: 1
If the user has set their browser to prompt for all downloads, I don't believe this is possible.
Upvotes: 0