gpt4 book ai didi

php - 如何将 Javascript 变量保存为客户端 PC 上的文件

转载 作者:可可西里 更新时间:2023-11-01 00:09:40 25 4
gpt4 key购买 nike

我将在编辑中发布我的代码...我在提交包含代码的问题时遇到了问题。

我维护一个聊天程序,当我被分配到该程序时,它已经构建好了。我的工作开始只是将其主题化以使其与我们网站的其余部分相匹配。现在管理层要求我提供额外的功能。其中一项功能是客户可以选择将聊天记录的副本保存到他们的计算机上。

整个聊天对话存储在一个名为 chatHistoryHTML 的 javascript 变量中,循环导致页面上的聊天 session 每 5 秒更新一次,方法是向 chatHistoryHTML 变量添加新的文本行并将其显示在“历史记录”div 中客户看到。

现在,我明白了如何打开一个新窗口来显示聊天记录,而不显示 Logo 、背景或文本输入框。但是,我无法使用我的方法将 PHP 命令传递到该新页面。

理想情况下,我想要一个允许用户单击按钮并出现一个保存对话框的解决方案,该对话框仅保存聊天对话,而无需打开新窗口。我现在正在打开一个新窗口,因为我试图避免保存页面上的所有其他内容。

我乐于接受建议。我懂一点 Javascript 和 PHP,但对 AJAX 一无所知

最佳答案

所有在客户端,你可以尝试:

var content, MIME_TYPE, theBlob, a;

// What will actually be put into the file
content = "THE FILE CONTENT";

// The file type
MIME_TYPE = "text/plain";
// Basically, the file itself
theBlob = new Blob([content], {type: MIME_TYPE});

// The anchor element
a = document.createElement("a");
// Set the name of the file that will be downloaded
a.download = "Chat_History.txt";
// Set the contents to be downloaded
a.href = window.URL.createObjectURL(theBlob);
// Anchor's text
a.textContent = "Download";

// What's displayed as the URL of the anchor (when hovered, copied, etc.)
a.dataset.downloadurl = [MIME_TYPE, a.download, a.href].join(":");

// Add the anchor to the page
document.body.appendChild(a);

演示: http://jsfiddle.net/oqskpydg/

这确实使用了并非在所有浏览器中都可用的功能,但它是一个可靠的选择。


引用资料:

关于php - 如何将 Javascript 变量保存为客户端 PC 上的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16197706/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com