Aximili
Aximili

Reputation: 29444

Force link to mp3 file to download/save-as

I have links to mp3 files on my website (normal <a href="file.mp3"> tags).

But for many users who have Apple Quicktime installed, it will open the mp3 files rather than "save" them when you click on the links.

Can you force the browser to save the link rather than using the browser preference?

I can use JavaScript or C# .NET and I'm looking for a simple solution if possible.

Upvotes: 2

Views: 9870

Answers (6)

jul28243
jul28243

Reputation: 21

It works with many browsers, but not Safari or IE: using the download attribute from HTML5

<a href="/audio/myfile.mp3" download>

Upvotes: 1

Dan F
Dan F

Reputation: 12052

You need to set a http header with "Content-Disposition" set to "attachment". This will force the browser to pop a save dialog. 873207 has a clean way of doing this in C#.

BUT, like Pax says, you probably shouldn't do this by default. A nice way of doing it is to provide a normal link to the file (the way you're doing it), and provide a secondary link beside it that forces the save dialog. That way you let the "less gifted" users do a simple click-to-save operation, as well as let every other user do whatever it is they want with the mp3.

Upvotes: 6

InternetPanda
InternetPanda

Reputation: 63

This is a rather outdated question and I'm not sure if there's any issues with doing things this way, but I had an issue recently and figured I'd post what I ended up using as a solution.

Save this as a php file (e.g. downloadmusicfile.php) in the same folder as your music:

<?php
$filename = $_GET['file'];
if (isset($filename))
{
// Prevents access to something that is not in the same directory as this script
if(substr_count($filename,"/") > 0)
{
die('This should not happen.');
};
$len = filesize($filename);
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: public');
header('Content-Description: File Transfer');
header('Content-Type: application/force-download');
header('Content-Disposition: attachment; filename='.$filename);
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.$len);
readfile($filename);
}
else
{
die('No file specified.');
};
?>

Then link to the files like so:

<a href="downloadmusicfile.php?file=song.mp3">Link Name</a>

This forces a "Save As" box when the user single/left clicks the download link.

Upvotes: 4

MiffTheFox
MiffTheFox

Reputation: 21555

Just add a Content-Disposition header to your page!

Here's a simple .htaccess rule to do this:

<FilesMatch "\.mp3$">
Header set Content-Disposition attachment; filename=downloaded.mp3
</FilesMatch>

The downside to this though is that your downloaded files will have the same name in the "save-as" dialog box. This might work without the filename part, but I haven't tested it. (Although I didn't test the rest of this as well... ;P )

Edit: Also, if you have server-side scripting available, you could use that to get the name of the file and set the Content-Disposition header.

Upvotes: 3

paxdiablo
paxdiablo

Reputation: 881103

I don't think you can do this easily. In addition, you shouldn't do this. The machine belongs to the user and, if they'd prefer to stream your MP3s rather than downloading them, it's their decision.

My own solution would be to provide a link for people to complain that they're streamed rather than downloaded (well, complain about anything, really) and let the community (including yourself) provide solutions.

Then, if someone states that it always seems to play rather than download, you can work with them to figure out they're using Firefox and QuickTime and tell them they can either right-click-save-as or reconfigure Firefox.

I will say that there will be ways in which you can achieve this, such as changing the MIME type or file name that's sent down, but my comment that this should be under the control of the user stands. And anyway, a determined user can quite easily re-configure their browser to stream whatever MIME type you decide to use so you still can't guarantee it.

Upvotes: 0

Andrew
Andrew

Reputation: 1873

It's impossible to absolutely force it, but you can try adding the line to your apache config:

AddType application/random-fake-type .mp3

It is my understanding that IE looks at a request and tries to guess the MIME type so the above workaround won't really work. You can also write a script to handle this for you, just like this similar php script.

Upvotes: 0

Related Questions