簡體   English   中英

Nodejs:通過循環鏈接數組下載多個文件時出錯

[英]Nodejs: Error while downloading multiple files by looping through array of links

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'
}]

下載此數組中的文件時,我正在獲取ENONENT:沒有這樣的文件或目錄,打開...

 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);

 };

試過目的地

 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);

 };

未捕獲的異常:錯誤:ENONENT:沒有這樣的文件或目錄,打開'C:\\ Users \\ Downloads \\ pics \\冬季在黃石國家公園.png蒸汽的野牛

在此輸入圖像描述 只需刪除'pics /'即可更改每個圖像的name屬性。 它會工作。

這是最終的代碼:

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();

正如@hoangdv所指出的,創建一個pics文件夾,並從正在傳遞給download_file_httpget str.name中刪除'pics/'

如果您創建名為 pics 的文件夾 ,則此完整示例可正常工作。

您還需要先安裝請求模塊。

npm install request --save

我沒有太大改變,除了:

  1. 格式化ultUrls聲明
  2. + str.name替換了代碼+ filename ,因為filename未定義。

這是代碼:

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();

我通過從網址中刪除'/ pics'解決了這個問題。

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'
}]

然后像這樣使用諾言

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);
});

參考: https//stackoverflow.com/a/36666258/7599905

這是因為您之前沒有創建過pics目錄

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM