Reputation: 548
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
Upvotes: 0
Views: 275
Reputation: 18487
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.
Upvotes: 1
Reputation: 722
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
}
});
Upvotes: 1