无法访问电子应用程序中asar存档中的文件 [英] Cannot access files inside asar archive in electron app
问题描述
电子版本:1.2操作系统:Windows
Electron version: 1.2 Operating system: Windows
无法使用readFileSync访问asar存档中的文件:
Cannot able to access the files inside asar archive using readFileSync:
var fs = require('fs');
var SQL = require('sql.js');
var filebuffer = fs.readFileSync('asar:'+ __dirname + './app/data/sample.db');
I've also tried using
trial 1 : readFileSync('D:/Sample-App/app.asar/app/data/sample.db');
trial 2 : readFileSync('./app/data/sample.db');
trial 3 : process.noAsar= true;
readFileSync('./app/data/sample.db');
没有一个试验成功.如果我尝试不使用app.asar,则可以访问db文件.因此,请帮助我解决此问题.
None of the trials worked out. If I try without using app.asar, I was able to access the db file. So Please help me resolve this issue.
推荐答案
我假设您的意思是,一旦将应用程序打包到asar上,文件路径就无法使用.
I'm assuming you mean that once you package your app to an asar, your file paths don't work.
fs.readFileSync('asar:'+ __dirname + './app/data/sample.db')
这很接近.您不需要"asar:",而应使用path模块使您的生活更轻松.
This is close. You don't need "asar:" and you should use the path module to make your life easier.
尝试使用 path
动态地构建路径.另外,您的主要入口点JS可能已在app文件夹中运行.尝试这样的事情:
Try constructing a path dynamically using path
. Also your main entry point JS is probably already running in the app folder. Try something like this:
path.join(__dirname, '/data/sample.db');
这将为每个操作系统创建一个有效的绝对路径(Windows中的路径使用\,而OSX和Linux中的路径使用/).请记住, __ dirname
是您当前目录的名称,因此之后的所有内容都必须相对于它.
This will create a valid, absolute path for each operating system (paths in Windows use \ whereas OSX and Linux uses /). Remember __dirname
is whatever your current directory is, so everything after that has to be relative to it.
这篇关于无法访问电子应用程序中asar存档中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!