单击数据 URI 锚链接时在 Chrome / Safari 中强制“另存为”框

发布于 2024-10-17 07:49:11 字数 4188 浏览 6 评论 0原文

<a href="data:text/csv;base64,CiJpZCIsInJlcXVlc3RfaWQiLCJkYXRlIiwiY29tcGFueV9uYW1lIiwiYWRkcmVzcyIsInBob25lIiwid2Vic2l0ZV91cmwiLCJwbGFjZV9wYWdlX3VybCIsImFkdmVydGlzZXIiLCJyZXZpZXdzIiwicmV2aWV3c190ZXh0Iiwib3duZXJfdmVyaWZpZWQiLCJjYXRlZ29yaWVzIiwibGF0IiwibG5nIiwicGxhY2VodG1sIiwiZ29vZ2xlX3RhZyIsIm1hcHNfdXJsIiwic2l0ZV9kZXNjcmlwdGlvbiIsImRlc2NyaXB0aW9uX2F0dHJpYnV0aW9uIiwib3duZXJfZW1haWwiLCJrZXl3b3JkIiwibGlua3MiCiIxNDc4IiwiNTMyIiwiIiwiU3RhcmJ1Y2tzIiwiMTI5OCBIb3dhcmQgU3RyZWV0LCBTYW4gRnJhbmNpc2NvLCBDQSA5NDEwMyIsIig0MTUpIDU2NS03Mzg1IiwiaHR0cDovL3d3dy5zdGFyYnVja3MuY29tLyIsImh0dHA6Ly9tYXBzLmdvb2dsZS5jb20vbWFwcy9wbGFjZT9obD1lbiZhc19xPXNhbitmcmFuY2lzY28rc3RhcmJ1Y2tzJmFzX2VwcT0mYXNfb3E9JmFzX2VxPSZudW09MTAmbHI9JmFzX2ZpbGV0eXBlPSZhc19zaXRlc2VhcmNoPSZhc19xZHI9YWxsJmFzX3JpZ2h0cz0mYXNfb2NjdD1hbnkmY3I9JmFzX25sbz0mYXNfbmhpPSZzYWZlPWltYWdlcyZ1bT0xJmllPVVURi04JnE9c2FuK2ZyYW5jaXNjbytzdGFyYnVja3MmZmI9MSZocT1zdGFyYnVja3MmaG5lYXI9U2FuK0ZyYW5jaXNjbywrQ0EmY2lkPTQ4NTI1NDg2NzIzODU5NzI1OTMmZWk9bUVWRFRhU05PWWEwbFFmV2haZ3Amc2E9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ZlPWltYWdlcyZ1bT0xJmllPVVURi04JnE9c2FuK2ZyYW5jaXNjbytzdGFyYnVja3MmZmI9MSZocT1zdGFyYnVja3MmaG5lYXI9U2FuK0ZyYW5jaXNjbywrQ0EmY2lkPTk1NjE5NTAwNjUwMTYwMTgwMCZlaT1tRVZEVGFTTk9ZYTBsUWZXaFpncCZzYT1YJm9pPWxvY2FsX3Jlc3VsdCZjdD1wbGFjZXBhZ2UtbGluayZyZXNudW09NCZ2ZWQ9MENEQVE0Z2t3QXciLCIwIiwiMzUiLCIiLCIxIiwiQ2FmZSwgQ29mZmVlICZhbXA7IFRlYSwgQ29mZmVlIFNob3BzLCBDb2ZmZWUgSG91c2VzICZhbXA7IENhZmVzIiwiIiwiIiwiIiwiMCIsIiIsIiIsIiIsIiIsInNhbiBmcmFuY2lzY28gc3RhcmJ1Y2tzIiwiQXJyYXkiCg==">Export This</a>

以上是包含 Base 64 编码的 csv 数据导出的数据 URI。在我的应用程序的报告页面上,我将导出嵌入到每个页面视图中,以便导出不需要再次访问数据库。

在 Firefox 3 中,单击此链接将打开另存为下载对话框。在 Chrome 9 中单击它不会执行任何操作。我可以在 Chrome 中右键单击“另存为”,它会将解码后的 csv 写入文件。在 Safari 中单击它会在浏览器窗口中打开解码的 CSV 数据,然后我可以手动保存这些数据。

在 IE 中...谁在乎呢,我说得对吗?

您可以通过将 标记完整复制到名为 test.html 的空白文件中并在每个浏览器中打开它来重现此内容以进行自己的测试。它的行为方式与我的应用程序上下文中的行为方式完全相同。

问题:当用户单击“数据 URI”链接时,有没有办法强制弹出“另存为”对话框,就像在 Firefox 中一样?或者有其他方法来解决这种不一致?

理论上,Firefox、Chrome 和 Safari 都支持 Data URI。在实践中......

<a href="data:text/csv;base64,CiJpZCIsInJlcXVlc3RfaWQiLCJkYXRlIiwiY29tcGFueV9uYW1lIiwiYWRkcmVzcyIsInBob25lIiwid2Vic2l0ZV91cmwiLCJwbGFjZV9wYWdlX3VybCIsImFkdmVydGlzZXIiLCJy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bGFjZXBhZ2UtbGluayZyZXNudW09NCZ2ZWQ9MENEQVE0Z2t3QXciLCIwIiwiMzUiLCIiLCIxIiwiQ2FmZSwgQ29mZmVlICZhbXA7IFRlYSwgQ29mZmVlIFNob3BzLCBDb2ZmZWUgSG91c2VzICZhbXA7IENhZmVzIiwiIiwiIiwiIiwiMCIsIiIsIiIsIiIsIiIsInNhbiBmcmFuY2lzY28gc3RhcmJ1Y2tzIiwiQXJyYXkiCg==">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.......

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

听闻余生 2024-10-24 07:49:11

不幸的是,application/octet-stream 在 Safari(至少 Safari 6)中工作得不太好。它只是将文件保存为“未知”,没有“保存”对话框。据我所知,除了从服务器弹回内容之外,没有跨浏览器的方法可以完成您想要做的事情。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文