坚持将二进制数据更改为base64(Gridfs-stream) [英] Stuck at changing binary data to base64 (Gridfs-stream)

查看:148
本文介绍了坚持将二进制数据更改为base64(Gridfs-stream)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一些二进制数据从上传的图像更改为base64,以便可以使用它来显示图像. 但是三进制给我这个错误:

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

我不明白,我发帖时也使用.on事件,它工作正常. 除此之外,我想知道我是否正确地更改了数据.

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 :)

我如何保存上传的图片(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);
});

在这里,我尝试获取图像并将二进制数据转换为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 + '">'
			})
		})
	})
});

我检查的资源:

  • gridfs-stream文档

  • gridfs-stream documentation

以GridFS中的HTML显示图像

推荐答案

我也在寻找从gridfs读取图像的解决方案,而且我正在使用grid-fs strem

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.

//设置gridfs

// 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);

//将图像写入mongo

// 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);
});

//将图像读取到mongo

// 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);
});

这篇关于坚持将二进制数据更改为base64(Gridfs-stream)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆