Home > Mobile >  Why do some images automatically download when their URL is visited and others not?
Why do some images automatically download when their URL is visited and others not?

Time:03-04

I feel like I should be able to figure this out, but I can't.

This image attempts to download (in the browsers I tested: safari and chrome):

https://d3i71xaburhd42.cloudfront.net/9470b0dc3daccafa53ebe8f54d5bfed00afce2ce/29-Figure13-1.png

while this one (and most other images) automatically appears in the browser:

https://i.imgur.com/TvqM9Gp.png

These two images are completely arbitrary examples; there are obviously many more examples one could point to. In my experience most images display automatically in the browser, but occasionally one wants to download to a 'downloads' folder or another familiar location, and I'm not quite sure why that is.

What causes some images to download while others are automatically opened by and are viewable in the browser?

CodePudding user response:

This is usually caused by either one of two HTTP response headers.

Content-Disposition

This header tells the browser that the content of the response should be displayed inline (the default value) or as an attachment. The latter will cause the browser to download the response.

Content-Type

This tells the browser what kind of content the response contains. Depending on the content type, the browser knows how a response should be handled. For example text/html will cause the browser to treat the response as HTML and will render it as such. text/plain will cause the response to be displayed as a simple text file, image/jpeg will cause the the response to be displayed as an image and binary/octet-stream will tell the browser, "this is binary data", which generally causes the browser to download the file. The list of MIME-types goes on and on.

If an image is downloaded instead of displayed in the browser and it doesn't have a Content-Disposition response header set to attachment, it usually means that the Content-Type isn't set correctly. For the first image you provided, the Content-Type is set to binary/octet-stream, so the browser will not treat it like an image.

  • Related