简体   繁体   中英

Stuck at changing binary data to base64 (Gridfs-stream)

I am trying to change some binary data from my uploaded images to base64 so I can use that to display an image. But the terimal is giving me this error:

TypeError: Cannot read property 'on' of undefined

I don't understand, when I post I also use the .on event and it is working fine. Besides that, I wonder if I am correctly changing the data.

Please take into account that I'm fairly new to node :)

How I save a uploaded image (POST)

 // Post to profile page router.post('/', function(req, res, next) { var busboy = new Busboy({ headers: req.headers }); busboy.on('file', function(fieldname, file, filename, encoding, mimetype) { var conn = mongoose.createConnection('192.168.99.100:32773'); conn.once('open', function () { var gfs = Grid(conn.db); var writestream = gfs.createWriteStream({ filename: filename, content_type: mimetype, mode: 'w', metadata: { belongs_to: req.session.passport.user } }); file.pipe(writestream); writestream.on('close', function(file){ res.render('profile', { user: req.user, message: req.flash('uploadMessage', 'Your image has been uploaded successfully!') }) }) }) }) req.pipe(busboy); }); 

Here I try to get a image and convert the binary data to base64 (GET)

 // GET to index router.get('/', function(req, res){ var conn = mongoose.createConnection('192.168.99.100:32773'); conn.once('open', function () { var gfs = Grid(conn.db); var readstream = gfs.createReadStream({ filename: 'kittendj.jpg' }); readstream.pipe(); readstream.on('open', function(chunk){ bufs.push(chunk); }) readstream.on('close', function(){ var bufs = []; var fbuf = Buffer.concat(bufs); var base64 = (fbuf.toString('base64')); res.render('index', { isAuthenticated: req.isAuthenticated(), user: req.user, imageSrc: '<img src="data:image/jpeg;base64,' + base64 + '">' }) }) }) }); 

Resources I checked:

I am also looking for the solution to read the image from gridfs and i am using grid-fs strem

this is the solution I found, hope it is helpful for you.

// set up the gridfs

import mongoose from 'mongoose';
import Grid from 'gridfs-stream';

const db = mongoose.connection.db;
const mongoDriver = mongoose.mongo;
const gfs = new Grid(db, mongoDriver);

// write the image to mongo

const writeStream = gfs.createWriteStream({
  filename: 'test.png',
  content_type: 'image/png',
});
fs.createReadStream(filePath).pipe(writeStream);

writeStream.on('close', (gfsFile) => {
  // remove the original file
  fs.unlink('test.png');
  // this is the information, and _id is the id 
  console.log(gfsFile);
});

// read the image to mongo

const readstream = gfs.createReadStream({
  _id: id,
});

const bufs = [];
readstream.on('data', function (chunk) {
  bufs.push(chunk);
});
readstream.on('end', function () {
  const fbuf = Buffer.concat(bufs);
  const base64 = fbuf.toString('base64');
  console.log(base64);
});

Please check this Github repo i've created to upload images and return back result as base64. https://github.com/houssem-yahiaoui/fileupload-nodejs .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM