使用Node.js将Base64图像转换为原始二进制文件

时间:2021-10-01 21:24:47

I have found posts that are close to what I'm looking for, but I have not been able to successfully implement what I want. Here is the general flow:

我发现的帖子与我正在寻找的内容很接近,但我无法成功实现我想要的内容。这是一般流程:

  1. Submit photo with rest of venue data, as base64 data
  2. 使用其余的场地数据提交照片,作为base64数据
  3. Strip data prefix if it exists, so I have just the image base64 data
  4. 剥离数据前缀(如果存在),所以我只有图像base64数据

var base64data = venue.image.replace(/^data:image\/png;base64,|^data:image\/jpeg;base64,|^data:image\/jpg;base64,|^data:image\/bmp;base64,/, '');

  1. Store Base64 data in GridFS via MongoDB (I'm using gridfstore)
  2. 通过MongoDB在GridFS中存储Base64数据(我正在使用gridfstore)
  3. Then, I'd like to retrieve the image upon request as a raw image file via a URL.
  4. 然后,我想通过URL将请求的图像作为原始图像文件检索。

// generic images route
server.get(version+'/images/:id', function(req, res) {
  gridfstore.read( req.params.id, function(error,data) {
    res.writeHead(200, {
      'Content-Type': 'image/jpeg',
      'Content-Length': data.buffer.length
    });

    res.end(data.buffer);
  });
});

Basically, this method returns the Base64 bytes stored in GridFS. I have tried other methods but they don't return the raw image.

基本上,此方法返回存储在GridFS中的Base64字节。我尝试了其他方法,但他们没有返回原始图像。

I'd like to pull up the image using URLs like this:

我想使用以下网址提取图片:

http://[localhost]/1/images/11dbcef0-257b-11e3-97d7-cbbea10abbcb

Here is a screenshot of the browser trace: 使用Node.js将Base64图像转换为原始二进制文件

以下是浏览器跟踪的屏幕截图:

2 个解决方案

#1


15  

You can take the string from MongoDB, create a new buffer instance, and specify an encoding when doing so. The resultant buffer will be in binary data.

您可以从MongoDB获取字符串,创建新的缓冲区实例,并在执行此操作时指定编码。结果缓冲区将是二进制数据。

var b64str = /* whatever you fetched from the database */;
var buf = new Buffer(b64str, 'base64');

So in your implementation:

所以在你的实现中:

server.get(version+'/images/:id', function(req, res) {
  gridfstore.read(req.params.id, function(err, data) {
    var img = new Buffer(data.buffer, 'base64');

    res.writeHead(200, {
      'Content-Type': 'image/jpeg',
      'Content-Length': img.length
    });
    res.end(img); 

  });
});

#2


0  

Make sure your string is correct. This worked for me..

确保你的字符串是正确的。这对我有用..

var buf = new Buffer(b64stringhere, 'base64');
var express = require('express'), app = express();
app.get('/img', function(r, s){
    s.end(buf);
})
app.listen(80);

#1


15  

You can take the string from MongoDB, create a new buffer instance, and specify an encoding when doing so. The resultant buffer will be in binary data.

您可以从MongoDB获取字符串,创建新的缓冲区实例,并在执行此操作时指定编码。结果缓冲区将是二进制数据。

var b64str = /* whatever you fetched from the database */;
var buf = new Buffer(b64str, 'base64');

So in your implementation:

所以在你的实现中:

server.get(version+'/images/:id', function(req, res) {
  gridfstore.read(req.params.id, function(err, data) {
    var img = new Buffer(data.buffer, 'base64');

    res.writeHead(200, {
      'Content-Type': 'image/jpeg',
      'Content-Length': img.length
    });
    res.end(img); 

  });
});

#2


0  

Make sure your string is correct. This worked for me..

确保你的字符串是正确的。这对我有用..

var buf = new Buffer(b64stringhere, 'base64');
var express = require('express'), app = express();
app.get('/img', function(r, s){
    s.end(buf);
})
app.listen(80);