电子应用程序createWriteStream抛出ENOENT错误 [英] Electron app createWriteStream throwing ENOENT error

查看:244
本文介绍了电子应用程序createWriteStream抛出ENOENT错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将文件下载到电子应用程序的文件系统中.我的代码在主线程中如下所示:

I'm trying to download files to the filesystem in an electron app. My code, in the main thread, looks like this:

const dir = `${__dirname}/media`;
if (!fs.existsSync(dir)){
    fs.mkdirSync(dir);
}

const file = fs.createWriteStream(`${dir}/${name}`);
file.on("open", function() {
    const request = http.get(url, function(response) {
        response.pipe(file);

        response.on('end', function() {
            file.close();
            ...
        });
    });

    request.on('error', function(err) {
        ...
    });
});

这在使用electron .在开发中运行时有效,但是当我使用electron-builder进行构建后,在警报中得到了错误:

This works when running in development using electron . But after I build it with electron-builder, I get the error in an alert:

未捕获的异常: 错误:在/Users/nicholasstephan/Desktop/XXXXXXX/dist/Mac/XXXXXX.app/Contents/Resources/app.asar中找不到ENOENT,media/uploads_2016_02_BASF_Holistic_Program.jpg 在notFoundError(ELECTRON_ASAR.js:109:19) 在Object.module.(匿名函数)[打开时](ELECTRON_ASAR.js:209:16) 在WriteStream.open(fs.js:1890:6) 在新的WriteStream(fs.js:1876:10) 在Object.fs.createWriteStream(fs.js:1831:10) 在下一个(/Users/nicholasstephan/Desktop/XXXXXXXX/dist/Mac/XXXXXXXX.app/Contents/Resources/app.asar/media.js:19:18) 在/Users/nicholasstephan/Desktop/XXXXXXXX/dist/Mac/XXXXXXXX.app/Contents/Resources/app.asar/media.js:52:4 ...

Uncaught Exception: Error: ENOENT, media/uploads_2016_02_BASF_Holistic_Program.jpg not found in /Users/nicholasstephan/Desktop/XXXXXXX/dist/Mac/XXXXXX.app/Contents/Resources/app.asar at notFoundError (ELECTRON_ASAR.js:109:19) at Object.module.(anonymous function) [as open] (ELECTRON_ASAR.js:209:16) at WriteStream.open (fs.js:1890:6) at new WriteStream (fs.js:1876:10) at Object.fs.createWriteStream (fs.js:1831:10) at next (/Users/nicholasstephan/Desktop/XXXXXXXX/dist/Mac/XXXXXXXX.app/Contents/Resources/app.asar/media.js:19:18) at /Users/nicholasstephan/Desktop/XXXXXXXX/dist/Mac/XXXXXXXX.app/Contents/Resources/app.asar/media.js:52:4 ...

其中引用的media.js ln 19是代码中的const file = fs.createWriteStream( $ {dir}/$ {name} );行.

where the media.js, ln 19, being referred to is the const file = fs.createWriteStream(${dir}/${name}); line in the code.

我已经尝试了其他十几种类似的stackoverflow答案中提供的解决方案,但是都没有解决该问题.

I've tried the solutions offered in about a dozen other similar stackoverflow answers, but none have fixed the problem.

这是怎么回事?

谢谢.

推荐答案

内置的Electron应用程序使用 Asar 格式. Asar是一种存档格式(实际上只是一个大文件),尽管在Electron中您可以从中读取文件,就好像它是标准目录一样.

The built Electron app uses the Asar format. Asar is an archive format (it's really just one big file) though in Electron you are able to read from it as if it were a standard directory.

我认为(尽管我没有看到它的明确记录),不可能用fs函数写入Asar.无论如何,几乎肯定会有更合适的位置来写入数据.

I presume (though I have not seen it explicitly documented) that it is not possible to write to an Asar with the fs functions. In any case there are almost certainly more appropriate locations to write data.

尝试写入其他路径. Electron使用 app.getPath(name) 提供了许多有用的路径,因此您可以例如,写入userData目录,其中包含您的应用程序的配置文件.

Try writing to a different path. Electron provides a number of useful paths using app.getPath(name) so you could for example write to the userData directory which holds configuration files for your app.

这篇关于电子应用程序createWriteStream抛出ENOENT错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆