Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript NodeJ将base64用作映像_Javascript_Node.js - Fatal编程技术网

Javascript NodeJ将base64用作映像

Javascript NodeJ将base64用作映像,javascript,node.js,Javascript,Node.js,我正在尝试将base64字符串作为带有image/png标题的图像。标题设置正确,但图像没有显示。我能看到的只是一个空白屏幕。代码如下: request('someCustomLink', function (error, response, body) { // someCustomLink gives the base64 string var img = new Buffer(body, 'base64'); res.writeHead(200, {

我正在尝试将base64字符串作为带有image/png标题的图像。标题设置正确,但图像没有显示。我能看到的只是一个空白屏幕。代码如下:

request('someCustomLink', function (error, response, body) {
// someCustomLink gives the base64 string
        var img = new Buffer(body, 'base64');
        res.writeHead(200, {
            'Content-Type': 'image/png',
            'Content-Length': img.length
        });
        res.end(img);         
});
是我得出这个解决方案所遵循的链接

任何帮助都将不胜感激

编辑 以下是来自我的someCustomLink的响应标题,它可能有助于理解问题betr

Accept-Ranges:bytes
Content-Length:128778
Content-Type:image-jpeg
Date:Thu, 21 Dec 2017 06:03:52 GMT
ETag:"edc04d469779108860478b361b7427d7"
Last-Modified:Mon, 11 Dec 2017 08:54:32 GMT
Server:AmazonS3
x-amz-id-2:QlR39g0vC5CeOo6fdKzX9uFB+uiOtj61c0LKW2rQLcCPWllcKyfbpg93Yido+vZfzMzB3lmhbNQ=
x-amz-request-id:3EC77634D9A05371
这是get请求

var request = require('request').defaults({ encoding: null });

app.get('/thumb/:thumbLink', function(req, res) {


        request('https://s3.amazonaws.com/my-trybucket/projectImages/'+req.params.thumbLink, function (error, response, body) {
            //console.log('error:', error); // Print the error if one occurred
            //console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
            //console.log('body:', body); // Print the HTML for the Google homepage.
            var img = new Buffer(body, 'base64');
            res.writeHead(200, {
                'Content-Type': 'image/png'

            });
            res.end(img);
        });
    });

-谢谢

您得到的回复包含数据:image/png;base64,。您必须在创建缓冲区之前删除此项。见下面的例子

request('https://s3.amazonaws.com/my-trybucket/projectImages/e31492f7314837a22a64395eee7cedfd', function(error, response, body) {
    var img = new Buffer(body.split(',')[1], 'base64');
    res.writeHead(200, {
      'Content-Type': 'image/png',
      'Content-Length': img.length 
    });
    res.end(img);
})

您得到的响应包含以下数据:image/png;base64,。您必须在创建缓冲区之前删除此项。见下面的例子

request('https://s3.amazonaws.com/my-trybucket/projectImages/e31492f7314837a22a64395eee7cedfd', function(error, response, body) {
    var img = new Buffer(body.split(',')[1], 'base64');
    res.writeHead(200, {
      'Content-Type': 'image/png',
      'Content-Length': img.length 
    });
    res.end(img);
})

你为什么会得到一份base64 PNG?这很不寻常。我有一个s3存储桶,其中包含base64 PNG格式的图像。您尝试过记录正文吗?是的,尝试过。它给出了base64字符串…这是我正在使用的链接,为什么要为您提供base64 PNG?这很不寻常。我有一个s3存储桶,其中包含base64 PNG格式的图像。您尝试过记录正文吗?是的,尝试过。它给出了base64字符串…这是我正在使用的链接,但这个Google徽标不是base64。我有customLink服务base64字符串,需要将其转换为图像。还尝试初始化请求,因为您告诉我结果是一样的。我看到的只是一个空白图像。您可以发布一个功能示例吗?添加了功能get req。您可以在系统上复制它。在上面的评论中找到答案的链接。还删除.defaults{encoding:null};如前所述。由于安全性和可用性问题,缓冲区已被弃用。请改用Buffer.alloc、Buffer.allocUnsafe或Buffer.from方法。但此Google徽标不是base64。我有customLink服务base64字符串,需要将其转换为图像。还尝试初始化请求,因为您告诉我结果是一样的。我看到的只是一个空白图像。您可以发布一个功能示例吗?添加了功能get req。您可以在系统上复制它。在上面的评论中找到答案的链接。还删除.defaults{encoding:null};如前所述。由于安全性和可用性问题,缓冲区已被弃用。请改用Buffer.alloc、Buffer.allocUnsafe或Buffer.from方法。