Using nw.js, I am just trying to save images in an array of img elements with different random names.
But having a few errors, is something wrong with my code?
for (i = 0; i < imgs.length; i++) {
request(imgs[i].getAttribute('src')).on('error', function(err) {
throw err
}).pipe(fs.createWriteStream('data/imgs/' + randomString))
}
imgs[] is an array of 100-500 html img element, but I am receiving
Error: EMFILE: too many open files, open *<directory>*
And another error:
"Uncaught Error: socket hang up"
Although it saves some images, some of them are corrupted, and it creates too many images than there actually is.
Use graceful-fs
module, which is a drop-in replacement for the fs
module. It is a wrapper around the native fs
module. Quoting the docs of graceful-fs
module,
Queues up
open
andreaddir
calls, and retries them once something closes if there is anEMFILE
error from too many file descriptors.
Since it exposes the same APIs as the native fs
module, you can use it just like the normal fs
module.
// use just like fs
var fs = require('graceful-fs');
Note: This library was created by, Isaac Z. Schlueter, one of the core developers of the Node.js.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With