How does the browser know what to do with the download link rather than redirecting?

I was wondering how the browser decides what to do when you provide a link that is actually intended to load, rather than redirecting to another page. How does the browser, for example, not redirect you to the page http: //domain/Music.mp3 , but simply drag the option to download the specified file? Does the browser see the type of content in the file when the user requests it, or does it look at the extension in the URL?

Any help is greatly appreciated.

Hi

+4
source share
4 answers

This applies to several things, the main one being the MIME type . In the response from the web server, it contains the string "Content-Type". Your browser will look at it and perform its default actions.

When I access the HTML page, the response contains:

Content-Type: text/html\r\n 

When you go to the .WMA file, the answer is: (I could only quickly find .WMA on my web server!)

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

The browser scans the Content-Type header to determine how to process the file. If for some reason the header is missing, some browsers try to guess the type of file from the extension, but I would not rely on it at all.

Edit: cf. also this question , published only yesterday.

+2
source

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

The most decisive title for the download is "Content-type". It tells the browser what the file is. If you do not have any installation that can run the file in a browser (for example, QuickTime, wmp or a PDF reader), it will load a download prompt.

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

+1
source

In addition to Content-Type, your browser will also examine the contents of the Content-Disposition HTTP header. Two headers control as follows:

  • If the Content-Disposition parameter is set to "attachment", the browser will always display a download prompt, regardless of whether the browser is capable of displaying the file within itself. This allows web developers to indicate that even displayed pages, such as HTML files or PDF files, should be loaded rather than displayed.
  • Otherwise, the browser will examine the contents of the Content-Type and determine if it is able to display the content or run a download prompt.

If you want to guarantee that a particular file should be considered as a download, you should not rely on Content-Type because you do not know what plug-ins the user has in their browser - they may be completely capable of displaying any file in their browser , and there is no way to determine that the user's browser behavior will only be with Content-Type.

+1
source

Source: https://habr.com/ru/post/1299739/


All Articles