简体   繁体   中英

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.

I can't use ' localStorage ' because the data might get deleted if the user deletes the cache.

I tried reading/writing using differant Modules, none of them worked mostly because of CROS. Using XMLHTTPrequests and Ajax doesn't work either and am running out of time.

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.

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. 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

 //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

The Electron API for appData does what you want. 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)

 name String 

Returns String - A path to a special directory or file associated with 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. 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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