Load image from local path (enabling --allow-file-access-from-files)


#1

Hi!
in my app i want to download some small profile picture of each user to display in the chat, to le the user know who your’re talking to.
i’ve already saved some files (like db, config.json, etc) in User’s document folder, so i’d like to download from my online server those picture ad use them in my chat page.

if i use i got an error “Not allowed to load from local path”.
i’ve set --allow-file-access-from-files when starting electron and set web-security to false, but it’s not working.
any other way to load images?!


#2

A sample code would be helpful


#3

// main.js

// express.js renders chats from sqlite file

restapi.get('/messages',(req,res)=>{
      db.all("SELECT * FROM CHAT", function(rows){
        res.render('chat',{
          data:rows,
          path:app.getPath('documents'), //here i store all data
        })
      })
    })

// chat.ejs renders all the chat messages

<table>
  <tr>
   <td class="picture">
    <div class="profilepicture"><img src="file:///<%= path %>/img/<%= data[i].MITTENTE %>.png" /> </div></td>
   <td class="messagebody"><%- data[i].MSG %></td>
  </tr>
  <tr>
   <td class="sender"><%= data[i].MITTENTE %>< /td>
   <td class="datetime"><%= data[i].DATA_ORA %>< /td>
  </tr>
</table>

the path in the chat.ejs file is valid, because if i copy it and paste in the browser it shows me the correct file.


#4

in the package.json file i run electron with

"scripts": { 
  "start": "electron . --allow-file-access-from-files"
}

and i set the web-security to false when i create the BrowserWindow

mainWindow = new BrowserWindow({
    width: 500,
    height: 640,
    titleBarStyle: 'hidden-inset',
    useContentSize:true,
    resizable: false,
    x: 0,
    y: 0,
    "webPreferences":{
      "webSecurity":false
    }
  })

#5

FOR ANYBODY WHO NEEDS
Resolved using express.js
i set a static folder as the user document folder, and used via staic urlo http://localhost:3000/path_to_file/file.jpg