简体   繁体   English

JavaScript 自动保存文本到客户端的文件

[英]JavaScript autosave text to file on client-side

I am using a code editor and a file view, I want whatever the user type in the code editor auto saves in the file.我正在使用代码编辑器和文件视图,我希望用户在代码编辑器中输入的任何内容都会自动保存在文件中。 I have a function to download the file with the current text inside like this:我有一个 function 来下载包含当前文本的文件,如下所示:

function saveFile() {
    var text = editor.doc.getValue();
    var text = text.replace(/\n/g, "\r\n");
    var blob = new Blob([text], {type: "text/x-python;charset=utf-8"});
    saveAs(blob, selectedFileName.name);
}

using a FileSaver.js, link: http://purl.eligrey.com/github/FileSaver.js .使用 FileSaver.js,链接: http://purl.eligrey.com/github/FileSaver.js

I store the files in a file array and display them as unordered list.我将文件存储在文件数组中并将它们显示为无序列表。

Currently I am replacing the whole file like this:目前我正在像这样替换整个文件:

fileList[0] = new File(["content"], "filename");

Can I update the text of a file in the fileList directly, without the need of creating a new file and replacing it?我可以直接更新 fileList 中文件的文本,而无需创建新文件并替换它吗?

You can use local sotage for store data at client side.您可以使用本地 sotage 在客户端存储数据。 // Store // 店铺

localStorage.setItem("lastname", "Smith");

// Retrieve document.getElementById("result").innerHTML = localStorage.getItem("lastname"); // 获取 document.getElementById("result").innerHTML = localStorage.getItem("lastname");

See this example here all about local storage with an example in simple words so please check it -- https://sknetking9.blogspot.com/2021/06/note-pad-tool-using-javascript-how-to.html请在此处查看此示例,所有关于本地存储的示例都用简单的话举例说明,因此请检查它 - https://sknetking9.blogspot.com/2021/06/note-pad-tool-using-javascript-how-to.html

Local storage set data,Get data and Delete data by js & html so i think its help full --本地存储集数据,通过js和html获取数据和删除数据,所以我认为它的帮助很充分——

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM