PDA

View Full Version : Force a link to download an MP3 rather than play it?




Mr Skills
Sep 18, 2008, 09:43 AM
I have put some free MP3s on my band's website (link in the sig) and hosted them on idisk, but when I click on them they play rather than downloading. It feels inelegant to zip them or ask people to right-click.

All the searches I have done on this topic suggest that it's just tough luck BUT.... if I host the MP3s on garageband.com they download, so they have obviously found some way round it.

website download link (http://idisk.me.com/ric.levy-Public/templescene/thestory.mp3)

garageband.com download link (http://www.garageband.com/mp3/Temple_Scene-The_Story.mp3?|pe1|S8LTM0LdsaSgYFi3Y2k)

The only obvious difference is that on garageband the song has "?|pe1|S8LTM0LdsaSgYFi3Y2k" after the filename on the link. Also, I've seen talk of changing MIME types so the browser doesn't know it's an MP3. Is this what I should do, and if so, how?

Thanks! :)



mshepherd
Sep 18, 2008, 10:06 AM
You have to change the content type in the header to force download. This will take some server side scripting. This is how i would do it in PHP, the application part of the content type forces it to be downloaded.

<?php
// We'll be outputting a MP3
header('Content-type: application/mp3');
// It will be called file.mp3
header('Content-Disposition: attachment; filename=file.mp3');
header('Content-Length: '.filesize("somefile.mp3"));
// The MP3 source is in somefile.pdf
//readfile("somefile.mp3");
readfile_chunked("somefilename.pdf");

function readfile_chunked($filename){
$chunksize = 1*(1024*1024); // how many bytes per chunk
$buffer = '';
$handle = fopen($filename, 'rb');
if ($handle === false) {
return false;
}
while (!feof($handle)) {
$buffer = fread($handle, $chunksize);
print $buffer;
}
return fclose($handle);
}
?>

Mr Skills
Sep 18, 2008, 11:19 AM
Thanks msheperd - the only problem (apart from my minimal paint-by-numbers HTML ability!) is that the website is actually a Blogger page, and the links are just HTML within a blog post... so I don't think I've got any server-side access.

Is it possible to put such headers/php within an HTML widget?

mshepherd
Sep 18, 2008, 01:15 PM
the only thing you may be able to do is change the mime types within the apache config if you have access to that.

Wee Beastie
Sep 18, 2008, 01:25 PM
In this case, since you say you don't have too much html/scripting experience, it might be best to just give your users instructions.

"Right click the links below and select 'Save Target As' to download to your hard drive" or something like that.

This will give them a choice--some people might rather stream it. Or this could be an opportunity to delve even deeper into the wonderful world o' web development. Good luck.

rickiac
Sep 18, 2008, 01:56 PM
Use FTP intead of HTTP as the source link, this should bring up the save / open dialog box.

Mr Skills
Sep 18, 2008, 05:55 PM
the only thing you may be able to do is change the mime types within the apache config if you have access to that.

It's a blogger blog, which just lets you put HTML into a box. So I don't think I've got access to things like Apache config (which I assume would be on the server).

In this case, since you say you don't have too much html/scripting experience, it might be best to just give your users instructions.

"Right click the links below and select 'Save Target As' to download to your hard drive" or something like that.

This will give them a choice--some people might rather stream it. Or this could be an opportunity to delve even deeper into the wonderful world o' web development. Good luck.

Well I know a little HTML... enough to follow instructions. It's just that it has to be relatively simple HTML/Javascript because it's going in a blogger widget. I've already got the "save as" instruction on there, but it's just so inelegant, especially since it's a slightly different instruction on every browser! (Which reminds me... bizarrely, sometimes you right-click on a song in Safari, the menu says "download linked file" and sometimes it says "save as" but it changes when I reload the page...)

Use FTP intead of HTTP as the source link, this should bring up the save / open dialog box.

Good suggestion... but it didn't work. The browser was just endlessly loading (maybe because iDisk is webdav and not FTP?).

Thanks for all the suggestions so far... keep them coming! :)

Wee Beastie
Sep 18, 2008, 06:39 PM
I've already got the "save as" instruction on there, but it's just so inelegant, especially since it's a slightly different instruction on every browser! (Which reminds me... bizarrely, sometimes you right-click on a song in Safari, the menu says "download linked file" and sometimes it says "save as" but it changes when I reload the page...)

This is true. Frankly I don't know the exact solution to your problem. Maybe if you zipped the mp3 before uploading to your blog. Then the browser won't be able to stream it. They'll have to download it and unzip it themselves (I think). Might work...