简体   繁体   English

使用JavaScript在本地应用程序(无服务器)中读取/写入和存储数据

[英]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. appDataElectron 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.

相关问题 如何使用JavaScript将JSON数据从远程URL读取到本地文件存储中的文件? - How to read JSON data from a remote URL to a file in the local file store using JavaScript? 如何读取文件,存储数据然后写入 - how to read a file, store data and then write it 使用JavaScript,可以在服务器上的文件中读取/写入文件 - With JavaScript is it possible to Read/Write from/to a file on the server Javascript函数将数据写入服务器 - Javascript function to write data on server 将 MongoDB 服务器数据同步到 IndexedDB 本地存储 - Synchronizing MongoDB server data to an IndexedDB local store 通过 JavaScript 将 Local Storage 数据写入 Cookie - Write Local Storage data into Cookie via JavaScript 是否有更好的方法来隐藏和存储JQuery / JavaScript应用程序的只读数据,而不是将它放在PHP数组中? - Is there a better way to hide & store read only data for a JQuery/JavaScript app than putting it in PHP arrays? 读取 JavaScript 中的数据并将其存储在 PostgreSQL 数据库表中 - Read data in JavaScript and store it in the PostgreSQL Database table 使用Chrome扩展名在JavaScript中读取和写入JSON数据? - Read and write json data in javascript with a chrome extention? 从 Javascript object 写入和读取数据 - Write and read data from Javascript object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM