将打开的网页保存为 HTML 文件

发布于 2024-10-28 08:34:40 字数 106 浏览 7 评论 0原文

我有一个 HTML 页面。 当我点击一个按钮时,我想触发一个“保存 HTML 页面”之类的东西,以便我能够保存打开的网页。我还想知道使用 jquery / javascript 是否可以实现这一点。

I have an HTML page.
When I click a button, I'd like to trigger a Save HTML page kinda thing so that I'll be able to save the opened web page. I'd also like to know if this is possible using the jquery / javascript.

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

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

发布评论

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

评论(2

驱逐舰岛风号 2024-11-04 08:34:40

HTML 页面无权访问文件系统。这将是一个安全问题。用户需要手动保存文件。

HTML pages do not have access to the filesystem. This would be a security issue. The user needs to save the file manually.

浅沫记忆 2024-11-04 08:34:40

对于 IE,这很简单,因为 IE 实现了 document.execCommand:

document.execCommand("SaveAs", false, "Test.html");

MSDN 有一个明确的 示例

如果您有兴趣让它在其他浏览器上工作,似乎没有任何客户端编程会调用“保存”对话框。

使用服务器端代码,您可以让按钮检索同一页面,并对随响应发送的 HTTP 标头进行以下更改:(

Content-disposition: attachment; filename=filename.ext

其中 filename.ext 显然是文件的名称)。

PHP 文档中 header() 函数的第一个 示例 展示了如何调用PDF 的下载对话框,Microsoft 还有另一个支持页面,其中包含 ASP 的示例。

With IE, it's simple as IE implements document.execCommand:

document.execCommand("SaveAs", false, "Test.html");

MSDN has an clear example of this.

If you're interested in having it work on other browsers, there doesn't seem to be any client-side programming that will invoke the Save dialogue box.

Using, server-side code, you can have the button retrieve the same page with the following change in the HTTP header sent with the response:

Content-disposition: attachment; filename=filename.ext

(where filename.ext is obviously the name of your file).

The first example in the PHP documentation for the header() function shows how to invoke the download dialog for a PDF, and Microsoft has another support page with an example for ASP.

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