本文介绍了保存与猫鼬形象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有关于这个主题相当多线程了,但遗憾的是我没有找到我的答案,直到如今。我用这个例子code angular.js从,以获得来自客户端的图像。这部分作品

I know there are quite a few threads on this topic already, but unfortunately I didn't find my answer until now. I use angular.js with the example code from http://angular-js.in/image-upload/ to get the image from the client. This part works

现在到节点/ mongodb的一部分,这里是我的后端模式:

Now to the node/mongodb part, here's my backend model:

var userSchema = new mongoose.Schema({
    avatar: { data: Buffer, contentType: String },
    // ...
});

module.exports = mongoose.model('User', userSchema);

和我的节点code:

exports.createAvatar = function (req, res) {
    var avatar = {
        data: req.body.data.image, // see below
        contentType:'image/png'
    }

models.DUser
        .findById(index.findUserId(req))
        .exec(
        function (err, user) {
user.avatar = avatar;
// ...
user.save(function (err, user) {/* ... */ });

和我angularCtrl:

and my angularCtrl:

var foo = {
  image: image,
  imageName: image.name,
};

$http.post('/api/users', {data: foo })
    .success(function (result) {
    /*...*/});

除了从req.body.data.image我试着像req.body.data.image.dataURL,req.body.data.image.dataURL.data许多不同的变化,但没有到目前为止的工​​作。我req.body.data.image的记录显示:

Beside from req.body.data.image I tried many different variations like req.body.data.image.dataURL, req.body.data.image.dataURL.data, but nothing worked so far. My Logging of req.body.data.image shows:

{ file:
   { webkitRelativePath: '',
     lastModified: 1411073963000,
     lastModifiedDate: '2014-09-18T20:59:23.000Z',
     name: '3770316278.png',
     type: 'image/png',
     size: 32493 },
  url: 'blob:http%3A//127.0.0.1%3A3000/cb20debc-8a3a-468f-ab5c-39299f7ec52b',
  dataURL: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAASwAAACHCAYAAAC.....

如何将图像保存到数据库?

How can I save the image to the database?

修改

我想一切后,的base64, req.body.data.image.dataURL 保存到头像像这样的:

I tried to save everything after base64, from req.body.data.image.dataURL into the avatar like this:

var split = req.body.data.image.dataURL.split('base64,');
var type = split[0];
var data = split[1];

var avatar = {
    data: type,
    contentType:'image/png'
};

保存邮件仍然是:

the save message is still:

user.avatar = avatar
user.save(function (err, user) {}

但我仍然得到错误类型错误:不能设置为空值的属性阿凡达

推荐答案

因为我的问题改了一下,我打这个作为解决,新的问题就在这里:Displaying从MongoDB的

since my question changed a bit, I'm marking this one as solved, the new question is here: Displaying Images in Angular.js from MongoDB

这篇关于保存与猫鼬形象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 18:07