更新
去年接受的答案很好,但今天我会使用其他人使用的套餐: https://github.com/mikeal/request
原始
我正试图抓取google的徽标并使用node.js将其保存到我的服务器。
这就是我现在所做的并且不起作用:
var options = {
host: 'google.com',
port: 80,
path: '/images/logos/ps_logo2.png'
};
var request = http.get(options);
request.on('response', function (res) {
res.on('data', function (chunk) {
fs.writeFile(dir+'image.png', chunk, function (err) {
if (err) throw err;
console.log('It\'s saved!');
});
});
});
我怎样才能使这个工作?
答案 0 :(得分:82)
这里发生了一些事情:
这应该有效:
var http = require('http')
, fs = require('fs')
, options
options = {
host: 'www.google.com'
, port: 80
, path: '/images/logos/ps_logo2.png'
}
var request = http.get(options, function(res){
var imagedata = ''
res.setEncoding('binary')
res.on('data', function(chunk){
imagedata += chunk
})
res.on('end', function(){
fs.writeFile('logo.png', imagedata, 'binary', function(err){
if (err) throw err
console.log('File saved.')
})
})
})
答案 1 :(得分:35)
这个帖子已经过时但我想用https://github.com/mikeal/request包做同样的事情。
这是一个工作示例
var fs = require('fs');
var request = require('request');
// Or with cookies
// var request = require('request').defaults({jar: true});
request.get({url: 'https://someurl/somefile.torrent', encoding: 'binary'}, function (err, response, body) {
fs.writeFile("/tmp/test.torrent", body, 'binary', function(err) {
if(err)
console.log(err);
else
console.log("The file was saved!");
});
});
答案 2 :(得分:26)
我建议您使用http-request,以便管理甚至重定向。
var http = require('http-request');
var options = {url: 'http://localhost/foo.pdf'};
http.get(options, '/path/to/foo.pdf', function (error, result) {
if (error) {
console.error(error);
} else {
console.log('File downloaded at: ' + result.file);
}
});
答案 3 :(得分:5)
这个怎么样?
var http = require('http'),
fs = require('fs'),
options;
options = {
host: 'www.google.com' ,
port: 80,
path: '/images/logos/ps_logo2.png'
}
var request = http.get(options, function(res){
//var imagedata = ''
//res.setEncoding('binary')
var chunks = [];
res.on('data', function(chunk){
//imagedata += chunk
chunks.push(chunk)
})
res.on('end', function(){
//fs.writeFile('logo.png', imagedata, 'binary', function(err){
var buffer = Buffer.concat(chunks)
fs.writeFile('logo.png', buffer, function(err){
if (err) throw err
console.log('File saved.')
})
})
答案 4 :(得分:0)
使用fs.readFileSync(./my_local_image_path.jpg)
这是为了从Azure Cognative Services's Vision API读取图像
const subscriptionKey = 'your_azure_subscrition_key';
const uriBase = // **MUST change your location (mine is 'eastus')**
'https://eastus.api.cognitive.microsoft.com/vision/v2.0/analyze';
// Request parameters.
const params = {
'visualFeatures': 'Categories,Description,Adult,Faces',
'maxCandidates': '2',
'details': 'Celebrities,Landmarks',
'language': 'en'
};
const options = {
uri: uriBase,
qs: params,
body: fs.readFileSync(./my_local_image_path.jpg),
headers: {
'Content-Type': 'application/octet-stream',
'Ocp-Apim-Subscription-Key' : subscriptionKey
}
};
request.post(options, (error, response, body) => {
if (error) {
console.log('Error: ', error);
return;
}
let jsonString = JSON.stringify(JSON.parse(body), null, ' ');
body = JSON.parse(body);
if (body.code) // err
{
console.log("AZURE: " + body.message)
}
console.log('Response\n' + jsonString);
答案 5 :(得分:0)
最干净的方法是使用request在本地保存图像:
const request = require('request');
request('http://link/to/your/image/file.png').pipe(fs.createWriteStream('fileName.png'))
如果您需要在标头中添加身份验证令牌,请执行以下操作:
const request = require('request');
request({
url: 'http://link/to/your/image/file.png',
headers: {
"X-Token-Auth": TOKEN,
}
}).pipe(fs.createWriteStream('filename.png'))