【问题标题】:Node.js Base64 Image decoding and writing to fileNode.js Base64 图像解码和写入文件
【发布时间】:2018-08-07 03:53:35
【问题描述】:

我正在将此 Flex 表单的内容(不要问为什么)发送到 node.js。有一个 post 参数叫做“photo”,它是一个 base64 编码的图像。

照片内容发送成功。问题是当我尝试解码内容并将它们写入文件时。

  var fs = require("fs");

  fs.writeFile("arghhhh.jpg", new Buffer(request.body.photo, "base64").toString(), function(err) {});

我也尝试过 toString("binary")。但似乎节点没有解码所有内容。它似乎只解码 jpg 标头信息并留下其余部分。

谁能帮我解决这个问题?

谢谢

【问题讨论】:

  • 这里的照片是什么??是base64图像???

标签: image apache-flex node.js base64 decoding


【解决方案1】:

尝试完全删除.toString(),直接写入缓冲区。

【讨论】:

  • 男孩,我觉得自己超级愚蠢。内森,你是英雄。谢谢。
  • 我很高兴 - 很高兴我能提供帮助:D
【解决方案2】:

这是我的完整解决方案,它可以读取任何 base64 图像格式,对其进行解码并以适当的格式保存在数据库中:

    // Save base64 image to disk
    try
    {
        // Decoding base-64 image
        // Source: http://stackoverflow.com/questions/20267939/nodejs-write-base64-image-file
        function decodeBase64Image(dataString) 
        {
          var matches = dataString.match(/^data:([A-Za-z-+\/]+);base64,(.+)$/);
          var response = {};

          if (matches.length !== 3) 
          {
            return new Error('Invalid input string');
          }

          response.type = matches[1];
          response.data = new Buffer(matches[2], 'base64');

          return response;
        }

        // Regular expression for image type:
        // This regular image extracts the "jpeg" from "image/jpeg"
        var imageTypeRegularExpression      = /\/(.*?)$/;      

        // Generate random string
        var crypto                          = require('crypto');
        var seed                            = crypto.randomBytes(20);
        var uniqueSHA1String                = crypto
                                               .createHash('sha1')
                                                .update(seed)
                                                 .digest('hex');

        var base64Data = 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAZABkAAD/4Q3zaHR0cDovL25zLmFkb2JlLmN...';

        var imageBuffer                      = decodeBase64Image(base64Data);
        var userUploadedFeedMessagesLocation = '../img/upload/feed/';

        var uniqueRandomImageName            = 'image-' + uniqueSHA1String;
        // This variable is actually an array which has 5 values,
        // The [1] value is the real image extension
        var imageTypeDetected                = imageBuffer
                                                .type
                                                 .match(imageTypeRegularExpression);

        var userUploadedImagePath            = userUploadedFeedMessagesLocation + 
                                               uniqueRandomImageName +
                                               '.' + 
                                               imageTypeDetected[1];

        // Save decoded binary image to disk
        try
        {
        require('fs').writeFile(userUploadedImagePath, imageBuffer.data,  
                                function() 
                                {
                                  console.log('DEBUG - feed:message: Saved to disk image attached by user:', userUploadedImagePath);
                                });
        }
        catch(error)
        {
            console.log('ERROR:', error);
        }

    }
    catch(error)
    {
        console.log('ERROR:', error);
    }

【讨论】:

  • 这是我正在寻找的回复,谢谢 m8
【解决方案3】:

在 nodejs 8.11.3 中,new Buffer(string, encoding) 已被弃用,取而代之的是Buffer.from(string, encoding) 始终没有.toString() 的新方法。 更多详情请阅读nodejs docs: Buffer中的文档

【讨论】:

    【解决方案4】:

    删除.toString()

    在这里,您将 base64 解码为缓冲区,这很好,但随后您将缓冲区转换为字符串。这意味着它是一个字符串对象,其代码点是缓冲区的字节。

    【讨论】:

      猜你喜欢
      • 2016-01-13
      • 2014-08-01
      • 2014-01-19
      • 1970-01-01
      • 2016-03-31
      • 2014-06-12
      • 1970-01-01
      • 2019-11-24
      • 2014-06-22
      相关资源
      最近更新 更多