Grant Zhu
Grant Zhu

Reputation: 3018

How does browser determine whether to download or show

I have a web resource which returns json content with Content-Type:application/json. Usually the content is displayed in browser directly but sometimes it's not, instead, a download prompt shows.

I know there's a header Content-Disposition:inline/attachment which can explictly tell browser whether to download or show. But if I don't specify this header, how does browser decide? What's its strategy?

Upvotes: 3

Views: 3948

Answers (2)

Turch
Turch

Reputation: 1546

From Mozilla's File types and download actions (emphasis mine):

When you click a link to download a file, the MIME type determines what action is taken. If you see an "Opening " dialog asking if you want to save the file or open it with a specified application, that normally means that your Mozilla application cannot handle the MIME type internally, no plugin is installed and enabled that can handle it and you have not previously selected a download action or helper application to always use for that type of file.

The browser comes preconfigured to handle basic formats like images. Plugins (which may be bundled with the browser) add handling for various common file types like pdfs. There can also be "helper applications", which means the browser downloads and forwards the file automatically to the application (such as a torrent magnet link opening your torrent client)

Everything else, it will ask until a user binds a default action (if the Content-Type is application/octet-stream, you can't set a default action). The other browsers work the same.

Upvotes: 4

Rodney G
Rodney G

Reputation: 4826

What Content-Type is specified in the header? If the browser doesn't know what it is, it probably defaults to application/octet-stream and prompts for download.

Here's a brief blog about it.

Upvotes: 1

Related Questions