[英]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'
}]
While downloading files in this array I'm getting ENONENT:no such file or directory, open ... 下载此数组中的文件时,我正在获取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);
};
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 未捕获的异常:错误:ENONENT:没有这样的文件或目录,打开'C:\\ Users \\ Downloads \\ pics \\冬季在黄石国家公园.png蒸汽的野牛
Just change the name attribute of each image by removing 'pics/'.
只需删除'pics /'即可更改每个图像的name属性。 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
正如@hoangdv所指出的,创建一个pics文件夹,并从正在传递给
download_file_httpget
str.name中删除'pics/'
This complete example works correctly, provided that you create a folder called pics
. 如果您创建名为
pics
的文件夹 ,则此完整示例可正常工作。
You'll need to install the request module first too. 您还需要先安装请求模块。
npm install request --save
I didn't change much except: 我没有太大改变,除了:
ultUrls
declaration ultUrls
声明 + filename
with + str.name
because filename
was undefined. + str.name
替换了代码+ filename
,因为filename
未定义。 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();
I solved the issue by removing '/pics' from the url. 我通过从网址中删除'/ 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'
}]
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 参考: https : //stackoverflow.com/a/36666258/7599905
这是因为您之前没有创建过pics
目录
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.