Tuesday, November 6, 2007

Forcing an "Open/Save As..." dialog from an Apache Web Server

Have you noticed that many times, when you click on a link to an MP3 or Quicktime file, your computer will automatically start to play that file in whatever your default media player is? I've been asked several times how to force media from a given web server to always give the "Open/Save As..." dialog, rather than to just start playing in the default media player.

The reason that your computer takes a certain action whenever you open a given file type is because of MIME types. Basically, the web server reports to your browser the type of information that it is sending. It knows this based on a file's extension. Your browser, in turn, knows that when it sees a certain MIME type that there is a corresponding program that it should use to handle files of that type. When you get the "Open/Save As..." dialog, it is generally because the server is sending a generic MIME type or because your computer has no program associated with the given MIME type. So, if we can forced the web server to send a generic MIME type for a certain type of file, every time a user accesses that file type, he or she will receive the "Open/Save As..." dialog.

This is pretty easy to accomplish in Apache. You can do so with a ".htaccess" file. Here's all you need to do:
1) in the directory where you would like to force the "Open/Save As..." dialog, just created a file called .htaccess
2) in the .htaccess file, add the text:
"AddType application/octet-stream mp3"
(minus the quotes) and save.

That's it! This example is for mp3 files, but you can substitute any extension for "mp3." Basically, what we have done is override Apache's MIME types just for the directory where you placed your .htaccess file and all of its subdirectories.

1 comment:

apelsin said...

Works for jpg in Firefox, but not in IE :(