PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, May 6, 2022

[FIXED] Why do some images automatically download when their URL is visited and others not?

 May 06, 2022     browser, google-chrome, image, safari     No comments   

Issue

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?


Solution

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.



Answered By - Ivar
Answer Checked By - Robin (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing