仅使用页面数据通过 Javascript 触发浏览器的另存为对话框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/4799669/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Trigger a Browser's Save-As Dialog via Javascript using only On-page data
提问by Ian
As part of my page's output, I'm including a CSV formatted version of a data table in a text area, so that the user can easily copy/paste a CSV export into their spreadsheet of choice.
作为页面输出的一部分,我在文本区域中包含了数据表的 CSV 格式版本,以便用户可以轻松地将 CSV 导出复制/粘贴到他们选择的电子表格中。
I would like a way to have a button that, when clicked, will trigger a Save-as dialog that will download the CSV contents already present in the textarea on the page.
我想要一种方法来拥有一个按钮,当点击该按钮时,将触发一个另存为对话框,该对话框将下载页面上文本区域中已经存在的 CSV 内容。
The intention is to avoid another trip to the database to get the CSV export, when it is already on the page, while providing the user a shortcut around having to create a new file and paste in the copied CSV.
目的是避免再次访问数据库以获取 CSV 导出,当它已经在页面上时,同时为用户提供一个快捷方式,无需创建新文件并粘贴到复制的 CSV 中。
Is this possible?
这可能吗?
采纳答案by robertc
I don't think it's currently possible to trigger save as without doing some sort of request back to the server, but it's possible to export data from a loaded page using a Data URI. Have a look at my answer to this similar question.
我认为目前不可能在不向服务器执行某种请求的情况下触发 save as ,但可以使用Data URI从加载的页面导出数据。看看我对这个类似问题的回答。

