0
var ultUrls = [{
  url: 'https://i.redd.it/kl44uq60z4631.png',
  name: 'pics/A bison in steam during winter at Yellowstone National Park.png'
}, {
  url: 'https://i.redd.it/9eocp20xr6631.jpg',
  name: 'pics/Athens at night.jpg'
}, {
  url: 'https://i.redd.it/0gezn9zjr6631.jpg',
  name: 'pics/This is Rayne. Her favorite color is orange. Her job is a clown for the circus. Her motto is *honk honk*..jpg'
}, {
  url: 'https://i.redd.it/fxcivargr6631.png',
  name: 'pics/Reddit your input is greatly appreciated.png'
}, {
  url: 'https://i.redd.it/xzpukskhr6631.jpg',
  name: 'pics/Cranberry lake Ontario.jpg'
}, {
  url: 'https://i.redd.it/4mplt3joh5631.jpg',
  name: 'pics/Pics of insects are the best.jpg'
}, {
  url: 'https://i.redd.it/03q4c5ndq6631.jpg',
  name: 'pics/Elvis Presley during his service in the U.S. Army.jpg'
}]

While downloading files in this array I'm getting ENONENT:no such file or directory, open ...

 var downloadImages = function(callback) {

    ultUrls.forEach( function(str) {

     download_file_httpget(str.url, str.name, function(){console.log('Finished Downloading' + filename)});
   });

 }
var download_file_httpget = function(url, dest, callback){

     request.get(url)
     .on('error', function(err) {console.log(err)} )
     .pipe(fs.createWriteStream(dest))
     .on('close', callback);

 };

Tried with destination

 var download_file_httpget = function(url, dest, callback){
     let destn = 'C:\\Users\\Downloads\\' + dest;
     request.get(url)
     .on('error', function(err) {console.log(err)} )
     .pipe(fs.createWriteStream(destn))
     .on('close', callback);

 };

UnCaught Exception: Error:ENONENT: no such file or directory, open 'C:\Users\Downloads\pics\A bison in steam during winter at Yellowstone National Park.png

imjared
  • 19,492
  • 4
  • 49
  • 72
Sai Krishna
  • 547
  • 1
  • 12
  • 26

4 Answers4

0

enter image description here Just change the name attribute of each image by removing 'pics/'. It will work.

Here is the final code:

const request = require('request');
const fs = require('fs');
var ultUrls = [ { url: 'https://i.redd.it/kl44uq60z4631.png', name: 'A bison in steam during winter at Yellowstone National Park.png' }, { url: 'https://i.redd.it/9eocp20xr6631.jpg', name: 'Athens at night.jpg' }, { url: 'https://i.redd.it/0gezn9zjr6631.jpg', name: 'This is Rayne. Her favorite color is orange. Her job is a clown for the circus. Her motto is honk honk..jpg' }, { url: 'https://i.redd.it/fxcivargr6631.png', name: 'Reddit your input is greatly appreciated.png' }, { url: 'https://i.redd.it/xzpukskhr6631.jpg', name: 'Cranberry lake Ontario.jpg' }, { url: 'https://i.redd.it/4mplt3joh5631.jpg', name: 'Pics of insects are the best.jpg' }, { url: 'https://i.redd.it/03q4c5ndq6631.jpg', name: 'Elvis Presley during his service in the U.S. Army.jpg' } ]

var downloadImages = function(callback) {

    ultUrls.forEach( function(str) {

     download_file_httpget(str.url, str.name, function(){console.log('Finished Downloading' + str.name)});
   });

 }

 var download_file_httpget = function(url, dest, callback){
     let destn = 'C:\\Users\\Downloads\\pics\\' + dest;
     request.get(url)
     .on('error', function(err) {console.log(err)} )
     .pipe(fs.createWriteStream(destn))
     .on('close', callback);

 };

 downloadImages();

As pointed out by @hoangdv create a pics folder and also remove 'pics/' from str.name that is being passed to download_file_httpget

harshrd
  • 79
  • 9
  • I don't think that there would be any problem passing `destn` to `fs.createWriteStream`. It looks fine to me. Why do you think that the variable is not getting passed? – Wyck Jun 24 '19 at 17:18
  • Also check to see if the destination path that you have specified before str.name is created already. I think that Downloads folder may not be created. – harshrd Jun 24 '19 at 17:27
  • You can use [`fs-extra.ensureDir`](https://www.npmjs.com/package/fs-extra) to create the dir if you want, too. – Wyck Jun 24 '19 at 17:35
0

This complete example works correctly, provided that you create a folder called pics.

You'll need to install the request module first too.

npm install request --save

I didn't change much except:

  1. formatted the ultUrls declaration
  2. replaced the code + filename with + str.name because filename was undefined.

Here's the code:

const request = require('request');
const fs = require('fs');

var ultUrls = [
  { url: 'https://i.redd.it/kl44uq60z4631.png', name: 'pics/A bison in steam during winter at Yellowstone National Park.png' },
  { url: 'https://i.redd.it/9eocp20xr6631.jpg', name: 'pics/Athens at night.jpg' },
  { url: 'https://i.redd.it/0gezn9zjr6631.jpg', name: 'pics/This is Rayne. Her favorite color is orange. Her job is a clown for the circus. Her motto is honk honk..jpg' },
  { url: 'https://i.redd.it/fxcivargr6631.png', name: 'pics/Reddit your input is greatly appreciated.png' },
  { url: 'https://i.redd.it/xzpukskhr6631.jpg', name: 'pics/Cranberry lake Ontario.jpg' },
  { url: 'https://i.redd.it/4mplt3joh5631.jpg', name: 'pics/Pics of insects are the best.jpg' },
  { url: 'https://i.redd.it/03q4c5ndq6631.jpg', name: 'pics/Elvis Presley during his service in the U.S. Army.jpg' }
];

var downloadImages = function (callback) {
  ultUrls.forEach(function (str) {
    download_file_httpget(str.url, str.name, function () { console.log('Finished Downloading' + str.name) });
  });

}
var download_file_httpget = function (url, dest, callback) {

  request.get(url)
    .on('error', function (err) { console.log(err) })
    .pipe(fs.createWriteStream(dest))
    .on('close', callback);

};

downloadImages();

Wyck
  • 10,311
  • 6
  • 39
  • 60
  • copy pasted your code. Still getting the same error. wth – Sai Krishna Jun 24 '19 at 17:43
  • installed request too – Sai Krishna Jun 24 '19 at 17:44
  • Is there any other way to download multiple files? – Sai Krishna Jun 24 '19 at 17:49
  • I don't believe I'm getting an accurate report from you. If you got `UnCaught Exception: Error:ENONENT: no such file or directory, open 'C:\Users\Downloads\pics\A bison in steam during winter at Yellowstone National Park.png` running this code, then it's because `C:\Users\Downloads\pics` doesn't exist. If you got it running this code then you've also set your current working directory to `C:\Users\Downloads` already, which is weird. Can you cut and paste the actual error you got? As it stands, so many of us are convinced that you just need to create the pics directory properly - I still am. – Wyck Jun 25 '19 at 12:20
  • I'm sorry for the confusion. I deleted the 'pics' part from the url. This was also helpful https://stackoverflow.com/a/36666258/7599905 – Sai Krishna Jun 25 '19 at 13:28
  • So what is your current status? Did you solve your problem? Is your original question answered? – Wyck Jun 26 '19 at 12:41
  • Yes. I've answered my question with the solution. Thank you for you help. I need to learn more about Async and Promises – Sai Krishna Jun 26 '19 at 16:02
0

I solved the issue by removing '/pics' from the url.

var ultUrls = [{
  url: 'https://i.redd.it/kl44uq60z4631.png',
  name: 'A bison in steam during winter at Yellowstone National Park.png'
}, {
  url: 'https://i.redd.it/9eocp20xr6631.jpg',
  name: 'Athens at night.jpg'
}, {
  url: 'https://i.redd.it/0gezn9zjr6631.jpg',
  name: 'This is Rayne. Her favorite color is orange. Her job is a clown for the circus. Her motto is *honk honk*..jpg'
}, {
  url: 'https://i.redd.it/fxcivargr6631.png',
  name: 'pics/Reddit your input is greatly appreciated.png'
}, {
  url: 'https://i.redd.it/xzpukskhr6631.jpg',
  name: 'Cranberry lake Ontario.jpg'
}, {
  url: 'https://i.redd.it/4mplt3joh5631.jpg',
  name: 'Pics of insects are the best.jpg'
}, {
  url: 'https://i.redd.it/03q4c5ndq6631.jpg',
  name: 'Elvis Presley during his service in the U.S. Army.jpg'
}]

Then used promise like so

Promise.each(ultUrls, url=> new Promise((resolve, reject) => {
    console.log('Downloading Image: ' + url.file_name);
    request(url.url).on('error', reject).pipe(fs.createWriteStream(path.join(__dirname, url.file_name))).on('finish', () => {
        console.log('Downloaded Image: ' + url.file_name);
        resolve();
    });
})).then(() => {
    console.log('All Image Downloaded!');
}).catch(err => {
    console.error('Failed: ' + err.message);
});

Reference: https://stackoverflow.com/a/36666258/7599905

Sai Krishna
  • 547
  • 1
  • 12
  • 26
0

It's because you haven't create the pics directory before

Théotime
  • 17
  • 5