最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Convert Base64 image to raw binary with Node.js - Stack Overflow

programmeradmin2浏览0评论

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. Strip data prefix if it exists, so I have just the image base64 data

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. Then, I'd like to retrieve the image upon request as a raw image file via a 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.

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:

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. Strip data prefix if it exists, so I have just the image base64 data

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. Then, I'd like to retrieve the image upon request as a raw image file via a 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.

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:

Share Improve this question edited Sep 28, 2015 at 20:03 hexacyanide 91.6k31 gold badges165 silver badges162 bronze badges asked Sep 25, 2013 at 1:47 remotevisionremotevision 4331 gold badge6 silver badges14 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 17

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.

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

So in your implementation:

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

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

  });
});

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);
发布评论

评论列表(0)

  1. 暂无评论