0

I'm trying to create a configuration for buttons with electron and shell. To open a file in a determinate location, for example, my file is situated on /Users/***/Desktop/CD-Przemek/documents/QuickStartGuideWN-H1.pdf.

I have already tried the solution inside the electron documentation and works pretty good, but works only on my mac and if I change the file location the button does not work anymore, I'm currently open for any solution. This is my current js code:(on index Html I have only a button)

const openBtn = document.getElementById('openBtn3')
const { shell, app } = require('electron')
const path = require('path');
const fs = require('fs')

openBtn.addEventListener('click', function(event){
  shell.openItem('document/documents/QuickStartGuideWN-H1.pdf') //this work really good only in debug mode

//A COUPLE OF SOLUTION TRIED

//var p = path.join(__dirname, '..' 'MAC_drivers')
//shell.openItem(app.getPath("~/Library/Desktop/CD-Przemek/documents/UserGuide.pdfWN-H1.pdf"))
//shell.openItem(app.getPath("desktop") + "/QuickStartGuideWN-H1.pdf")
})

I just need a code that can find the folder or the file on all PCs and on all platforms, and maybe open it. Thanks all

Romy
  • 498
  • 9
  • 14
  • I found a solution that works great [link](https://stackoverflow.com/questions/46022443/electron-how-to-add-external-files) Thanks everyone for help I really appreciate – Romy Aug 30 '19 at 16:37

2 Answers2

1

By default Electron has the same restrictions as a normal web browser, means your renderer can't just open local files.

If you absolutely must use the renderer process for this and have no other way than putting the files under the user's documents folder then you can try to disable the webSecurity on your BrowserWindow object like this:

win = new BrowserWindow({
    webPreferences: {
        webSecurity: false
    }
});
andypotato
  • 703
  • 5
  • 10
  • Tanks for your answer, but not solve my problem, I need the button to find and open the folder wherever it is, in this case, work only in document folder – Romy Aug 30 '19 at 12:06
1

I just need a code that can find the folder or the file on all PCs and on all platforms, and maybe open it. Thanks all

Unless the file is in known, default locations on the platforms you want to support, how do you see this working?

Seems like the only option is put up an "open dialog" and let the user find the file.

spring
  • 18,009
  • 15
  • 80
  • 160
  • Thanks for your answer, I know I tried to use 'showOpenDialog' and works but do not find the correct path if I change the file location. Did you know a method to include the file inside the electron app after the creation of pkg or exe? – Romy Aug 30 '19 at 15:32
  • Is the file you want to open INSIDE or OUTSIDE the app bundle? You can include any files you want INSIDE the app bundle and open them without user intervention. I suggest you edit your question to make it clearer what you are trying to do –  to give folks a better chance of being able to help you. – spring Aug 30 '19 at 16:14
  • Thanks, you are right but I think if there is a solution for my previews problem and there is another path to explore I will explore it. My final goal is to open this file, inside or outside is the same for me – Romy Aug 30 '19 at 16:22