Daud
Daud

Reputation: 7877

Difference between Content-Disposition: attachment and <a>'s download attribute

When the response header of an HTTP request is Content-Disposition: attachment; filename=image.gif, the response body is downloaded by the browser as "image.gif". The same seems to be achieved when an anchor tag has the download enter code hereattribute set to "image.gif".

What's the difference between the two then? Is the Content-Disposition: attachment method no longer of much use in browsers that support the "download" attribute on anchor tags?

Upvotes: 2

Views: 1614

Answers (1)

indigo
indigo

Reputation: 1137

Content-Disposition is still useful.

The download attribute will take precedence over the Content-Disposition for the filename:

If the Content-Disposition header has different information from the download attribute, resulting behavior may differ: If the header specifies a filename, it takes priority over a filename specified in the download attribute. If the header specifies a disposition of inline, Chrome, and Firefox 82 and later, prioritize the attribute and treat it as a download. Firefox versions before 82 prioritize the header and will display the content inline.

MDN

You do not have to use HTML5 download attribute however, so Content-Disposition is another way the server can decide the filename without client direction.

Also of note is that the download attribute only works same-origin, so it will not work without Content-Disposition attachment if the origins are different.

Upvotes: 2

Related Questions