Node.js 使用请求模块从 url 下载图像给出 URI 错误
Node.js downloading images from url using request module give URI error
我正在循环下载图像,当我将循环设置为 for (var i = 0; i < 100; i++) {
时,我可以毫无问题地下载图像。但是一旦我将 i
范围更改为超过 99(包括 100),那么任何类似:for (var i = 100; i < 200; i++) {
或 for (var i = 0; i < 200; i++) {
我都会收到错误消息。这是我的代码:
var fs = require('fs');
var request = require('request');
var download = function(uri, filename, callback){
request.head(uri, function(err, res, body){
request(uri).pipe(fs.createWriteStream(filename)).on('close', callback);
});
};
var arrLength = allProducts.length;
for (var i = 0; i < arrLength; i++) {
let imgName = allProducts[i].code;
let imgUrl = allProducts[i].nofrillsImgLink
download(`${imgUrl}`, `./allProductsImg/${imgName}.png`, function(){
});
};
这是我收到的错误:
events.js:287
throw er; // Unhandled 'error' event
^
Error: Invalid URI "undefined"
at Request.init (.../node_modules/request/request.js:273:31)
at new Request (.../node_modules/request/request.js:127:8)
at request (.../node_modules/request/index.js:53:10)
at Request._callback (.../downloadImg.js:7:5)
at self.callback (.../node_modules/request/request.js:185:22)
at Request.emit (events.js:310:20)
at Request.init (.../node_modules/request/request.js:273:17)
at new Request (.../node_modules/request/request.js:127:8)
at request (.../node_modules/request/index.js:53:10)
at Function.head (.../node_modules/request/index.js:61:12)
Emitted 'error' event on Request instance at:
at Request.init (.../node_modules/request/request.js:273:17)
at new Request (.../node_modules/request/request.js:127:8)
[... lines matching original stack trace ...]
at Function.head (.../node_modules/request/index.js:61:12)
你能从
更改此代码吗
var download = function(uri, filename, callback){
request.head(uri, function(err, res, body){
request(uri).pipe(fs.createWriteStream(filename)).on('close', callback);
});
};
至
var download = function(uri, filename, callback){
if(uri !== undefined){
request.head(uri, function(err, res, body){
request(uri).pipe(fs.createWriteStream(filename)).on('close', callback);
});
}
};
将代码更改为此并且有效:
for (var i = 0; i < 11952; i++) {
let imgName = allProducts[i].code;
let imgUrl = allProducts[i].nofrillsImgLink
request({
url : `${imgUrl}`,
//make the returned body a Buffer
encoding : null
}, function(error, response, body) {
//will be true, body is Buffer( http://nodejs.org/api/buffer.html )
console.log(body instanceof Buffer);
//do what you want with body
//like writing the buffer to a file
fs.writeFile(`./allProductsImg/${imgName}.png`, body, {
encoding : null
}, function(err) {
if (err)
throw err;
console.log('It\'s saved!');
});
});
};
我正在循环下载图像,当我将循环设置为 for (var i = 0; i < 100; i++) {
时,我可以毫无问题地下载图像。但是一旦我将 i
范围更改为超过 99(包括 100),那么任何类似:for (var i = 100; i < 200; i++) {
或 for (var i = 0; i < 200; i++) {
我都会收到错误消息。这是我的代码:
var fs = require('fs');
var request = require('request');
var download = function(uri, filename, callback){
request.head(uri, function(err, res, body){
request(uri).pipe(fs.createWriteStream(filename)).on('close', callback);
});
};
var arrLength = allProducts.length;
for (var i = 0; i < arrLength; i++) {
let imgName = allProducts[i].code;
let imgUrl = allProducts[i].nofrillsImgLink
download(`${imgUrl}`, `./allProductsImg/${imgName}.png`, function(){
});
};
这是我收到的错误:
events.js:287
throw er; // Unhandled 'error' event
^
Error: Invalid URI "undefined"
at Request.init (.../node_modules/request/request.js:273:31)
at new Request (.../node_modules/request/request.js:127:8)
at request (.../node_modules/request/index.js:53:10)
at Request._callback (.../downloadImg.js:7:5)
at self.callback (.../node_modules/request/request.js:185:22)
at Request.emit (events.js:310:20)
at Request.init (.../node_modules/request/request.js:273:17)
at new Request (.../node_modules/request/request.js:127:8)
at request (.../node_modules/request/index.js:53:10)
at Function.head (.../node_modules/request/index.js:61:12)
Emitted 'error' event on Request instance at:
at Request.init (.../node_modules/request/request.js:273:17)
at new Request (.../node_modules/request/request.js:127:8)
[... lines matching original stack trace ...]
at Function.head (.../node_modules/request/index.js:61:12)
你能从
更改此代码吗var download = function(uri, filename, callback){
request.head(uri, function(err, res, body){
request(uri).pipe(fs.createWriteStream(filename)).on('close', callback);
});
};
至
var download = function(uri, filename, callback){
if(uri !== undefined){
request.head(uri, function(err, res, body){
request(uri).pipe(fs.createWriteStream(filename)).on('close', callback);
});
}
};
将代码更改为此并且有效:
for (var i = 0; i < 11952; i++) {
let imgName = allProducts[i].code;
let imgUrl = allProducts[i].nofrillsImgLink
request({
url : `${imgUrl}`,
//make the returned body a Buffer
encoding : null
}, function(error, response, body) {
//will be true, body is Buffer( http://nodejs.org/api/buffer.html )
console.log(body instanceof Buffer);
//do what you want with body
//like writing the buffer to a file
fs.writeFile(`./allProductsImg/${imgName}.png`, body, {
encoding : null
}, function(err) {
if (err)
throw err;
console.log('It\'s saved!');
});
});
};