Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.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
Image Node.js Base64图像解码和写入文件_Image_Apache Flex_Node.js_Base64_Decoding - Fatal编程技术网

Image Node.js Base64图像解码和写入文件

Image Node.js Base64图像解码和写入文件,image,apache-flex,node.js,base64,decoding,Image,Apache Flex,Node.js,Base64,Decoding,我将这个弹性表单的内容(不要问为什么)发送到node。有一个称为“photo”的post参数,它是一个base64编码的图像 照片的内容发送过来了。问题是当我试图解码内容并将其写入文件时 var fs = require("fs"); fs.writeFile("arghhhh.jpg", new Buffer(request.body.photo, "base64").toString(), function(err) {}); 我也尝试过字符串(“二进制”)。但node似乎并没有

我将这个弹性表单的内容(不要问为什么)发送到node。有一个称为“photo”的post参数,它是一个base64编码的图像

照片的内容发送过来了。问题是当我试图解码内容并将其写入文件时

  var fs = require("fs");

  fs.writeFile("arghhhh.jpg", new Buffer(request.body.photo, "base64").toString(), function(err) {});
我也尝试过字符串(“二进制”)。但node似乎并没有解码所有的内容。它似乎只解码jpg头信息,并离开其余的

有人能帮我吗


谢谢

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

删除
.toString()


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

这是我的完整解决方案,可以读取任何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);
    }

在nodejs 8.11.3中,不推荐使用新的缓冲区(字符串,编码),而新的方法是使用
Buffer.from(字符串,编码)
始终不使用
.toString()

要了解更多详细信息,请阅读

boy do I feel super Dumby中的文档。内森,你是个英雄。谢谢。我很高兴能帮上忙:这里有什么照片??那是base64图像吗???这是我搜索的响应,谢谢