First, look at the effect drawing

Two, installation and usestoreRead and save the path

1, Why not use window.localstorage

LocalStorage only works in the browser process (the renderer process). LocalStorage is not very fault tolerant, so if your application encounters an error and exits unexpectedly, you may lose data. LocalStorage supports only persistent strings. This module supports any JSON supported type. LocalStorage is not very secure and may leak information due to an XSS attack. The electron- Store module has a better API. You can set and get nested properties. You can set the default initial configuration.

2. The difference between Vuex and storage

Vuex is stored in the memory, and localstorage is stored locally as a file. The electron store data store still exists after the application is uninstalled. Application scenario: Vuex is used to transfer values between components, and localStorage is used to transfer values between different pages. Permanent: Vuex stores lost values when the page is refreshed, but localStorage does not.

Note: Many students think that using LocalStorage can replace VUEX. It can be used for invariable data. However, when two components share a data source (object or array), if one component changes the data source and expects the other component to respond to the change, Localstorage cannot do so, because of difference 1.

3. Install the electron store

npm install electron-store

Note: Electron 5 or higher is required. If the installation fails, you can run the CNPM install electrostore-store command instead (if CNPM is installed).

4. electron store

const Store = require('electron-store');

const store = new Store();

store.set('unicorn'.'🦄');
console.log(store.get('unicorn'));
/ / = > '🦄'

// Use dot notation to access nested properties
store.set('foo.bar'.true);
console.log(store.get('foo'));
//=> {bar: true}

store.delete('unicorn');
console.log(store.get('unicorn'));
//=> undefined
Copy the code

Three, electron usedialog.showOpenDialogOpen folder

  • OpenDirectory – allows folder selection
  • Res.filepaths [0] — The selected folder path

Detailed configuration, please refer to https://www.electronjs.org/docs/api/dialog#dialogshowopendialogbrowserwindow-options

How to download network files and change the downloaded file name

You can usefs.createWriteStreamSave files in the form of file streamsParameters that

  • Url – The network address of the file to download
  • Filename — the filename
  • Savepath – The path to the file to save
  • Callback – Returns the callback function

The entire code is as follows