无法在mongodb中存储base 64图像

时间:2016-08-19 08:57:12

标签: angularjs node.js mongodb express base64

我需要在mongodb

中存储使用网络摄像头js捕获的基本64位图像数据

我尝试将图像数据存储在mongodb中但无法这样做

服务器

模式:

var mongoose = require('mongoose'),
    Schema = mongoose.Schema;

var ProfilesSchema = new Schema({
    name: String,
    otherFiles: [Object]
});

module.exports = mongoose.model('Profiles', ProfilesSchema);

表达js:

exports.otherFiles = function(req, res) {
    console.log("b4" + req.body.key.imgDataField);
    var base64Image = new Buffer(req.body.key.imgDataField, 'binary').toString('base64');
    req.body.key.imgDataField = base64Image;
    console.log("after" + req.body.key.imgDataField);
    Profiles.update({
        "_id": req.params.id
    }, {
        $push: {
            "otherFiles": {
                imgDataField: req.body.key.imgDataField
            }
        }
    }, function(error, profiles) {

        if (error) {

        }
        return res.status(200).json(fnStruncturedData(profiles));

    });
};

客户端

 controller:

$scope.take_snapshot = function() {
    debugger;
    Webcam.snap(function(data_uri) {
        $scope.data = data_uri;
        $scope.oData = {};
        $scope.oData.imgDataField = data_uri;
        getCandidateInterviewListService.fnSavefiles(localStorage.getItem('candidateID'), $scope.oData).then(function(response) {});
        document.getElementById('my_result').innerHTML = '<img src="' + data_uri + '"/>';
        //console.log($scope.data);
    });
    $scope.set = true;
}

 service:

    this.fnSavefiles = function(id, sData) {
        debugger;
        return ajaxServiceManager.fnQuery({
            sUrl: 'http://192.168.208.31:4000/onboardvue/profiles/otherFiles/' + id,
            sMethod: "PUT",
            oData: {
                key: sData
            }
        });
    };

请帮我解决这个问题 我正在使用mongodb,表达js

1 个答案:

答案 0 :(得分:0)

我存储了这样的基础64图像:

<强> MyController.js

exports.addBook = function (req, res) {
  'use strict';

  var book = new Book({
    title    : req.body.title,
    author   : req.body.author,
    category : req.body.category,
    synopsis : req.body.synopsis,
    units    : req.body.units,
    avatar: {
      data       : base64Image('your path' + req.body.avatar + '.jpg'),
      contentType: 'image/png'
    }
  });

  function base64Image(src) {
    return fs.readFileSync(src).toString("base64");
  }

  book.save(function (err, books) {
    if (err) {
        return res.status(500).send(err.message);
    }
    res.status(200).jsonp(books);
  });
};

希望它可以帮到你