Home > Article > Web Front-end > How to Force Browser to Download Images on Click?
Introduction
When clicking on image files in a browser, the default behavior is usually to open the image in a new tab or window. However, in some cases, you may want to force the browser to download the image instead.
Client-Side Programming
Yes, it is possible to achieve this using client-side programming, primarily with HTML5.
HTML5 Solution
The HTML5 attribute download provides an elegant way to force image downloads. Here's how to use it:
<a href="image.jpg" download>Download Image</a>
Behavior
By adding the download attribute to an anchor element, the browser prompts to download the image file with its original name. The user can specify a custom name through the download attribute value.
Limitations
It's important to note that the download attribute is only supported by modern browsers. Additionally, for cross-origin image requests (loading images from a different domain), the download attribute is not supported in modern browsers as of spring 2018 due to security concerns.
The above is the detailed content of How to Force Browser to Download Images on Click?. For more information, please follow other related articles on the PHP Chinese website!