使用node.js存储图像会产生错误 [英] Storing images using node.js gives error

查看:145
本文介绍了使用node.js存储图像会产生错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 wikimedia 获取图片,图片的链接是

I am trying to get an image from wikimedia and the link of the image is

upload.wikimedia。 org /wikipedia / commons / thumb / e / e3 /AybükeArslan_(3).JPG / 220px-AybükeArslan_(3).JPG

但是当我尝试的时候要使用 node.js 获取图像,维基媒体服务器会将错误声明作为响应。但是在浏览器上,URL完美运行并将图像作为响应。

But when i try to get the image using node.js the wikimedia server gives error statement as response. But on the browser the URL works perfectly and give the image as response.

这是错误陈述


400错误请求服务器无法遵守请求,因为
格式错误或其他错误。无法解码请求

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.')
        })
    })

})

有人可以帮忙吗?坚持了很长时间。

Can anybody help? stuck in to it for a long time.

推荐答案

我问了一个单独的问题来找到答案,隐藏的字符存在一些问题,这些字符在崇高中是不可见的,导致错误,在我提到的代码完全正确的问题中,有关详细信息,请参阅以下答案

I Asked a separate question to find the answer, There is some problem with hidden characters which are not visible in sublime, That causes the error, In the question the code i mentioned was completely right, For further details read below answer

字符串有隐藏字符,无法验证,

这篇关于使用node.js存储图像会产生错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆