2

I am building a electron app with vue-cli-electron-builder.

User submits form with name, email and image(photo) which is stored in local mysql database and later on it will be synchronize to cloud. Only image name will be stored in the database, the problem is

  • where and how do I save the actual image locally ?
  • how do i synchronize image to cloud ?

In development mode it was stored inside the project but production bundled app doesn't work like that.

I tried multer.diskStorage to save image to './server/uploads'. It works in development mode but in production it doesn't work.

Bipin Shrestha
  • 404
  • 4
  • 14

1 Answers1

0

You can store uploaded in userAppData folder. You should try like as below

function uploadFile() {
    dialog.showOpenDialog({
          properties: ['openFile','multiSelections'],
          filters: [{
             name: 'Images',
             extensions: ['jpg', 'png', 'gif']
          }]
       },
       uploadF, //define callback 
    )
}
function uploadF(filePaths) {
    if (filePaths!=undefined) {
        //multiple image upload
        for (let i = 0; i < filePaths.length; i++) {
            let fileName = path.basename(filePaths[i])
            fileName = moment().unix()+'-'+fileName //rename file
            let fileUploadPath = app.getPath('userData')+ '' + fileName;
            move(filePaths[i],fileUploadPath,cb)
        }
    }
}
function cb(e) {
    console.log("error in upload file",e);
}
function move(oldPath, newPath, callback) {
    fs.rename(oldPath, newPath, function (err) {
        if (err) {
        if (err.code === 'EXDEV') {
            copy();
        } else {
            console.log("err",err);

            callback(err);
        }
        return;
        }
        callback();
    });
    function copy() {
        var readStream = fs.createReadStream(oldPath);
        var writeStream = fs.createWriteStream(newPath);
        readStream.on('error', callback);
        writeStream.on('error', callback);

        readStream.on('close', function () {
            let fileName=path.basename(newPath)
            console.log(fileName,"  file uploaded")
            //remove path from destination
            //fs.unlink(oldPath, callback);
            // do your stuff
        });
        readStream.pipe(writeStream);
    }
}
Mayank Vadiya
  • 1,437
  • 2
  • 19
  • 32