繁体   English   中英

如何使用multer上传文件并将详细信息存储在mean rest应用程序的数据库中,以及如何从angular.js检索文件

[英]How to upload file using multer and store details in database in mean rest app and retrieve it from angular.js

我正在通过我的其他应用程序上传文件,但是无法从backend取回文件。 我如何检索它,这是上传代码

var storage = multer.diskStorage({
    destination: function (req, file, callback) {
      callback(null, './uploads');
    },
    filename: function (req, file, callback) {
      callback(null, mongoose.Types.ObjectId() + '-' + file.originalname);
          }
  });
  var upload = multer({ storage : storage }).array('userPhoto',10);
  upload(req,res,function(err) {
    console.log(req.files);
    var images =[];
    for(var i=0; i<req.files.length; i++){
      images[i]=req.files[i].path;
    }
        var newalbum = new albummodel({
           image:images
        });
        newalbum.save(function(err, albm) {
           if(err) {
              res.json({success: false, msg: 'can't store.'});
           } else {
            console.log(albm);
          }
        });
    if(err) {
      return res.end("Error uploading file.");
    }
    res.end("File is uploaded");
  });

这是我的检索代码

albummodel.findOne({_id:req.params.id},function(err, docs){
    res.json(docs);
 })

整个讨论对您有帮助。 请参考。

https://github.com/Automattic/mongoose/issues/3079

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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