download instead of opening files

Safari ignores the download directive and treats the tag metioned below as a standard href and open it. This makes posting a file dialog to the user from html element.click() impossible. The only method is to option click or right click and do "Download Link As..." on an existing link.


If the user forgets to hold down option, then that Safari opens the file. Even disabling the preference to automatically open "safe" files, Safari still opens them. It also unzip's zip archives. This would be allevieated by providing download support above, since then the proper file dialog behavior from Chome/Firefox would occur.


----------------------------------------

Repost from the archives... This was posted (and unanswered) back in May of 2015.


I'm trying to allow the user to save a dynamically generated 'file'.


What I have works in Chrome/OSX, Chrome/Ubuntu, and Firefox/Ubuntu. The user is presented with a browser 'save file' dialog. Not so on Safari, which displays the content of the 'file'.


The element I am generating is like this:

<a download="file.json" href="data:application/octet-stream,...."></a>


I create that, then add it to the document body, click it, and then remove it.


How can I get Safari to behave the same as the other browsers?

Replies

>The only method is to option click or right click and do "Download Link As..." on an existing link.

I think the download/save as link is the only option? After all, users are trained to follow that process, right?

There is no existing link in this case. This is a download button with <a href="url" download="name"> with js to execute element.click(). Since there's no way to perform an option-click from js programatically you can't post a file dialog. I ended up using a target to push the content to another tab. Then the user has to go to the tab, select save page as, select source instead of webarchive in the file popup, and then save it from there.


That's pretty convoluted if you ask me for a platform that prides itself on user interface simplicity, but better than getting the previous tab content kicked out and replaced with the saved content. Hitting the back button to return to previous tab content isn't always cheap or even possible. IE has the some of the same problems, but has msSaveBlob as a workaround. It also doesn't open content into the same tab by default. Safari doesn't have anything to solve these issues simply as far as I can tell.

Seems that Safari still can't download some blobs (but still tries to display them even if they fail like .zip files). Also the blob name is used for the displayed content instead of the download attribute, so that's also super cryptic for the users. These reports have occurred for years, so I anticipate no resolution to Apple adding the download attribute. The best solution recommended was converting to a data: url and base64 encoding the content stream. That bloats the content needlessly, and still doesn't preserve the filename.