Search code examples
htmlsafarihref

Safari: in pinned tab, download attribute with data href not working


In Safari 11/12, I have an tag with href="data:" and attribute "download" to save the file when the link is clicked.

<!DOCTYPE html>
<html><body>
<a href="data:text/plain;charset=utf-8,Hello" download="hello.txt">
    Click me
</a>
</body></html>

This works fine in Chrome and Firefox, and also in an unpinned tab in Safari. Clicking on the link downloads the text "Hello" into a new file called hello.txt. However, in a pinned tab in Safari, the download attribute seems to be ignored and Safari opens the data in a new tab.

To see this you need to serve the above HTML code from a web server. If you just save the file and open it with a "file://" URL, it works as expected.

I have tried using a Blob instead but the behaviour was the same.

I have tried changing the mimeType to "application/octet-stream" which was a suggestion I found to a similar question. This does cause the data to be downloaded, but the filename is always "unknown.txt". This isn't suitable as I need to have control over the filename and extension (it's not hello.txt!)

As an extra note, the behaviour is actually different in Safari 11 and 12 but neither is working properly. In 11, the data is opened in a new tab. In 12, it opens a new tab but shows an error opening the data.

This looks like a bug in Safari to me. Can anyone suggest a workaround?

Thanks


Solution

  • It appears it is a well known bug and the download attribute is not yet supported by Safari (webkit).

    https://bugs.webkit.org/show_bug.cgi?id=167341

    https://caniuse.com/#feat=download