[英]Read/Write and store data internelly in a Local App (no server) with JavaScript
So I am making a local App using Javascript , React and Electron and I want it to be able to work just fine without internet. 所以我使用Javascript,React和Electron制作本地应用程序,我希望它能够在没有互联网的情况下正常工作。
I can't use ' localStorage ' because the data might get deleted if the user deletes the cache. 我不能使用' localStorage ',因为如果用户删除缓存,数据可能会被删除。
I tried reading/writing using differant Modules, none of them worked mostly because of CROS. 我尝试使用不同的模块进行读/写,其中没有一个主要是因为CROS。 Using XMLHTTPrequests and Ajax doesn't work either and am running out of time.
使用XMLHTTPrequests和Ajax也不起作用,并且时间不多了。
When I use them on the test server, they return the index.html for the mainpage (They can at least access that ... and still they can't read the data) but when I try it on the build I get CORS the error. 当我在测试服务器上使用它们时,它们返回主页的index.html(它们至少可以访问...但仍然无法读取数据)但是当我在构建中尝试它时,我得到了CORS错误。
My Idea for now is to enable CORS on my webpage since I have no worries about security : The App will run ONLY offline so there is no danger. 我现在的想法是在我的网页上启用CORS,因为我不担心安全问题:应用程序将仅在线下运行,因此没有危险。 But After many hours...I didn't find a solution to do it on the client side.
但经过几个小时......我没有找到解决方案在客户端做到这一点。
If anyone has an idea or suggestion I would be grateful. 如果有人有想法或建议,我将不胜感激。
I tried : fs,FileReader,FileSaver, $.ajax,XMLHTTPrequests 我试过:fs,FileReader,FileSaver,$ .ajax,XMLHTTPrequests
//using $ajax
var test = $.ajax({
crossDomain:true,
type: 'GET',
url:'../data/DefaultCategorie.txt',
contentType: 'text/plain',
success: function(data){
console.log(data);
},
error: function(){
alert('failed');
},
})
//using fs
fs.readFile('../data/DefaultCategorie.txt', 'utf8', (err, data) => {
if (err) {
console.log("Failed");
throw err
}
console.log(data);
fs.close(data, (err) => {
if (err) throw err;
});
});
This article covers the 3 most common ways to store user data: How to store user data in Electron 本文介绍了存储用户数据的3种最常用方法: 如何在Electron中存储用户数据
The Electron API for appData
does what you want. appData
的Electron API appData
您的需求。 It is very easy to use. 这是非常容易使用。
From the above article: 从上面的文章:
const userDataPath = (electron.app || electron.remote.app).getPath('userData');
this.path = path.join(userDataPath, opts.configName + '.json')
this.data = parseDataFile(this.path, opts.defaults);
function parseDataFile(filePath, defaults) {
try {
return JSON.parse(fs.readFileSync(filePath));
} catch(error) {
// if there was some kind of error, return the passed in defaults instead.
return defaults;
}
}
Docs 文件
app.getPath(name)
app.getPath(名称)
name String
Returns String - A path to a special directory or file associated with name.
返回String - 指向与name关联的特殊目录或文件的路径。 On failure, an Error is thrown.
失败时,抛出错误。
You can request the following paths by the name:
您可以通过名称请求以下路径:
appData - Per-user application data directory, which by default points to: %APPDATA% on Windows $XDG_CONFIG_HOME or ~/.config on Linux ~/Library/Application Support on macOS userData - The directory for storing your app's configuration files, which by default it is the appData directory appended with your app's name.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.