使用 node.js 存储图像会出错
Storing images using node.js gives error
我正在尝试从 wikimedia 获取图像,图像的 link 是
upload.wikimedia.org/wikipedia/commons/thumb/e/e3/AybükeArslan_(3).JPG/220px-AybükeArslan_(3).JPG
但是当我尝试使用 node.js 获取图像时,wikimedia 服务器 给出了错误声明作为响应。但是在浏览器上 URL 可以完美地工作并给出图像作为响应。
这是错误的说法
400 Bad Request The server could not comply with the request since it
is either malformed or otherwise incorrect. Failed to decode request
密码是
var http = require('https')
, fs = require('fs')
, options
options = {
host: 'upload.wikimedia.org'
, port: 443
, path: '/wikipedia/commons/thumb/e/e3/AybükeArslan_(3).JPG/220px-AybükeArslan_(3).JPG'
}
var request = http.get(options, function(res){
var imagedata = ''
res.setEncoding('binary')
res.on('data', function(chunk){
imagedata += chunk
})
res.on('end', function(){
console.log(imagedata);
})
//trying to store the response as a jpg image which i am failing miserably because the response is a error statement rather than image stream.
res.on('end', function(){
fs.writeFile('image.jpg', imagedata, 'binary', function(err){
if (err) throw err
console.log('File saved.')
})
})
})
有人可以帮忙吗?坚持了很长时间。
因为您在 url 中有未编码的字符串。使用 encodeURI
已更新
var http = require('https')
, fs = require('fs')
, options;
options = {
hostname: 'upload.wikimedia.org'
, port: 443
, path: encodeURI('/wikipedia/commons/thumb/e/e3/AybükeArslan_(3).JPG/220px-AybükeArslan_(3).JPG')
};
var request = http.get(options, function(res){
var imagedata = '';
res.setEncoding('binary');
res.on('data', function(chunk){
imagedata += chunk
});
res.on('end', function(){
console.log(imagedata);
});
//trying to store the response as a jpg image which i am failing miserably because the response is a error statement rather than image stream.
res.on('end', function(){
fs.writeFile('image.jpg', imagedata, 'binary', function(err){
if (err) throw err;
console.log('File saved.');
});
})
});
我问了一个单独的问题来寻找答案,隐藏字符在 sublime 中不可见存在一些问题,导致错误,在问题中我提到的代码是完全正确的,有关详细信息,请阅读下文回答
我正在尝试从 wikimedia 获取图像,图像的 link 是
upload.wikimedia.org/wikipedia/commons/thumb/e/e3/AybükeArslan_(3).JPG/220px-AybükeArslan_(3).JPG
但是当我尝试使用 node.js 获取图像时,wikimedia 服务器 给出了错误声明作为响应。但是在浏览器上 URL 可以完美地工作并给出图像作为响应。
这是错误的说法
400 Bad Request The server could not comply with the request since it is either malformed or otherwise incorrect. Failed to decode request
密码是
var http = require('https')
, fs = require('fs')
, options
options = {
host: 'upload.wikimedia.org'
, port: 443
, path: '/wikipedia/commons/thumb/e/e3/AybükeArslan_(3).JPG/220px-AybükeArslan_(3).JPG'
}
var request = http.get(options, function(res){
var imagedata = ''
res.setEncoding('binary')
res.on('data', function(chunk){
imagedata += chunk
})
res.on('end', function(){
console.log(imagedata);
})
//trying to store the response as a jpg image which i am failing miserably because the response is a error statement rather than image stream.
res.on('end', function(){
fs.writeFile('image.jpg', imagedata, 'binary', function(err){
if (err) throw err
console.log('File saved.')
})
})
})
有人可以帮忙吗?坚持了很长时间。
因为您在 url 中有未编码的字符串。使用 encodeURI
已更新
var http = require('https')
, fs = require('fs')
, options;
options = {
hostname: 'upload.wikimedia.org'
, port: 443
, path: encodeURI('/wikipedia/commons/thumb/e/e3/AybükeArslan_(3).JPG/220px-AybükeArslan_(3).JPG')
};
var request = http.get(options, function(res){
var imagedata = '';
res.setEncoding('binary');
res.on('data', function(chunk){
imagedata += chunk
});
res.on('end', function(){
console.log(imagedata);
});
//trying to store the response as a jpg image which i am failing miserably because the response is a error statement rather than image stream.
res.on('end', function(){
fs.writeFile('image.jpg', imagedata, 'binary', function(err){
if (err) throw err;
console.log('File saved.');
});
})
});
我问了一个单独的问题来寻找答案,隐藏字符在 sublime 中不可见存在一些问题,导致错误,在问题中我提到的代码是完全正确的,有关详细信息,请阅读下文回答