Javascript HTML:如何创建“另存为”按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2849500/
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
HTML: how to create a "save as" button?
提问by His
In your browser, when you want to save an HTML page that you are currently viewing, you normally go to the File menu and click Save As.
在浏览器中,当您想要保存当前正在查看的 HTML 页面时,通常会转到“文件”菜单并单击“另存为”。
Can I have a little button at the bottom of an HTML page that does the same thing? So instead of going to the File menu -> Save As, I want my user to be able to click the button to save the page on to the disk.
我可以在 HTML 页面底部有一个小按钮来做同样的事情吗?因此,与其转到文件菜单 -> 另存为,我希望我的用户能够单击按钮将页面保存到磁盘上。
There is a solution exists using Javascript as far as I know, but it only works for IE. See here: link text
据我所知,有一个使用 Javascript 的解决方案,但它只适用于 IE。请参阅此处:链接文本
采纳答案by Alex K.
You could have the link run a server side script that loads the HTML file and writes it back to the client with a Content-Disposition: attachment; filename=xxx.htmlheader.
您可以让链接运行一个服务器端脚本,该脚本加载 HTML 文件并将其写回带有Content-Disposition: attachment; filename=xxx.html标头的客户端。
回答by Sarfraz
The document.execCommand('SavaAs')works only in IE but the following link suggests other possibilities you may want to try out.
将document.execCommand('SavaAs')只能在IE,但下面的链接推荐您可能会想尝试其他的可能性。
回答by YOU
Take a look at downloadifyjQuery plugin, which using flash to save. Javascript alone is impossible.
看看downloadifyjQuery插件,它使用flash来保存。单靠 Javascript 是不可能的。
回答by code guy
You have to create a button that downloads the HTML file, or the page you're on:
您必须创建一个按钮来下载 HTML 文件或您所在的页面:
<form><input type="button" value="Download Now" onClick="window.location.href='yourpage.html'"></form>

