views:

50

answers:

4

Hi,

I was wondering how a browser decides what to do when you provide a link that is actually meant for downloading not redirecting to another page. How does the browser know for example not to redirect you to a page called http://domain/Music.mp3 rather that just throw the option to download the said file instead? Does the browser look at the content type on the file when the user requests it or does it look at the extension in the url?

Any help much appreciated.

Regards

+2  A: 

The browser looks at the Content-Type header to determine how to handle a file. If the header is not there for some reason, some browsers do try to guess the file type from the extension, but I wouldn't generally rely on that.

Edit: cf. also this question posted just yesterday.

RegDwight
+1  A: 

This is down to a number of things, the main one being MIME type. In the response from the webserver, it contains the line "Content-Type". Your browser will look at this and perform its default actions.

When I access a HTML page the response contains:

Content-Type: text/html\r\n

When you go to a .WMA file, the response is: (I could only quickly find a .WMA on my webserver!)

Content-Type: audio/x-ms-wma\r\n
Wil
+1  A: 

Apache sends headers with a file so that the browser knows what to do with the file.

The most deciding header for downloading is 'Content-type'. It tells the browser what kind of file it is. If you don't have anything install that can run the file within the browser(like quicktime, wmp, or a pdf reader) it will load the download prompt.

For more information on headers: http://en.wikipedia.org/wiki/List_of_HTTP_headers

RJD22
+1  A: 

In addition to Content-Type, your browser will also look at the content of a HTTP header called Content-Disposition. The two headers control things as follows:

  • If Content-Disposition is set to "attachment", the browser will always display a download prompt, regardless of whether the browser is capable of displaying the file internally. This allows web developers to specify that even displayable pages like HTML files or PDFs should be downloaded rather than displayed.
  • Otherwise, the browser will look at the content of Content-Type, and determine whether it is capable of displaying the content or if it should launch a download prompt.

If you want to guarantee that a particular file should be treated as a download, you shouldn't rely on Content-Type, because you don't know what sort of plugins the user has on their browser - they may be perfectly capable of displaying any kind of file within their browser, and there's no way to determine what the behaviour of the user's browser will be with Content-Type alone.

Ryan Brunner