Interacting with local files



  • Hi!

    Hope this is the correct forum to post this question.

    I created a simple web app that generates { entries } and save them in an [ array ]. All this works perfectly in the browser.

    My next step is to integrate ‘fs’ (or similar) to be able to work with real data. Basically my idea is to generate the data and being able to save it into the desktop. At the same time to open the generated file (json) and render it for further edits.

    Any tips on how to approach this problem? I’m aware fs can only run in the electron side – but I can’t find a lot of documentation on how to set this up and still interact with it through the vue/client side.

    Thanks!


  • Admin

    Hi,

    So are you building an Electron app or are you making a website?



  • Hi!

    Building an Electron App with ‘fs’ capabilities – would like to use the Quasar framework for this project. Thanks!



  • Don’t have time to give you a proper exemple now, sorry for that, but I think you have to use : https://electron.atom.io/docs/api/ipc-main/

    Its a process that send message from main process to renderer process (or renderer to main). So if you want to use fs node module you can do something like (didn’t test it, just a quick heads up) :

    // In main process.
    const {ipcMain} = require('electron')
    const fs = require('fs')
    const path = require('path')
    
    ipcMain.on('get-json', (event, filePath) => {
     const formatedPath = path.join(__dirname, filePath)
     fs.readFile(formatedPath, 'utf8', (err, data) => {
        if (err) 
           // error handling
    
        const jsonFile = JSON.parse(data)
        event.sender.send('get-json-reply', jsonFile)
      })
    })
    
    ipcMain.on('generate-data', (event, data, filePath) => {
      const formatedPath = path.join(__dirname, filePath)
      fs.writeFile(formatedPath, JSON.stringify(data, null, 2) , 'utf-8', (err) => {
         if (err) 
           // error handling
          event.sender.send('generate-data-reply')
      });
    })
    
    // In renderer process (web page).
    const {ipcRenderer} = require('electron')
    
    ipcRenderer.send('generate-data', {hello: 'world'}, './test/test.json');
    
    ipcRenderer.on('generate-data-reply', (event, arg) => {
       // do whaterver you want
      console.log(ipcRenderer.sendSync('get-json', './test/test.json'));
    })
    



Log in to reply
 

Looks like your connection to Quasar Framework was lost, please wait while we try to reconnect.