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ZyYW5jaXNjbytzdGFyYnVja3MmZmI9MSZocT1zdGFyYnVja3MmaG5lYXI9U2FuK0ZyYW5jaXNjbywrQ0EmY2lkPTk1NjE5NTAwNjUwMTYwMTgwMCZlaT1tRVZEVGFTTk9ZYTBsUWZXaFpncCZzYT1YJm9pPWxvY2FsX3Jlc3VsdCZjdD1wbGFjZXBhZ2UtbGluayZyZXNudW09NCZ2ZWQ9MENEQVE0Z2t3QXciLCIwIiwiMzUiLCIiLCIxIiwiQ2FmZSwgQ29mZmVlICZhbXA7IFRlYSwgQ29mZmVlIFNob3BzLCBDb2ZmZWUgSG91c2VzICZhbXA7IENhZmVzIiwiIiwiIiwiIiwiMCIsIiIsIiIsIiIsIiIsInNhbiBmcmFuY2lzY28gc3RhcmJ1Y2tzIiwiQXJyYXkiCg==">Export This</a>

The above is a Data URI containing a base 64 encoded csv data export. On my app's report page, I embed the export within each page view so that an export doesn't require another trip to the database.

In Firefox 3, clicking this link opens a save-as download dialog box. Clicking it in Chrome 9 does nothing. I can right-click-save-as in Chrome, and it will write the decoded csv to a file. Clicking it in Safari opens the decoded CSV data in the browser window, which I can then save manually.

In IE... who cares, am I right?

You can reproduce this to test for yourself by copying that <a> tag in it's entirety into a blank file called test.html and open it in each browser. It acts exactly the same way as it does in the context of my app.

Question: Is there a way to force a Save As dialog to pop up, as in Firefox, when the user clicks the Data URI link? Or some other way to get around this inconsistency?

In theory, Firefox, Chrome and Safari all support Data URIs. In practice.......

1条回答
SAY GOODBYE
2楼-- · 2019-04-26 18:16

Unfortunately, application/octet-stream doesn't work too well in Safari (at least Safari 6). It just saves the file as "unknown" with no Save dialog box. To my knowledge, there's no cross-browser way to do what you're trying to do except to bounce the content off a server.

查看更多
登录 后发表回答