如何使用nodejs将图像上传到文件夹并将路径保存到mongodb?

时间:2015-07-21 08:42:00

标签: angularjs node.js html5 mongodb express

我想使用angularjs或html5上传图片,Express.jsjs和mongodb.i已经按照教程我无法将图片上传到上传文件夹请一些人帮我怎么做。我有没有给出我的架构代码

my schema
var UserSchema = new Schema({
    UserName: {
        type: String,
        default: '',
        trim: true,
    },
    Avatar: {
        type: String,
        default: '',
        trim: true

    },
    Email: {
        type: String,
        default: '',
        trim: true
    },
    Password: {
        type: String,
        default: '',
        trim: true
    }
});

我想上传包含用户详情的图片

<form action="/upload" method="post" enctype="multipart/form-data">
    Select file to upload:
    <input type="file" name="Avatar" id="fileToUpload">
    <input type="text" name="UserName" id="name">
    <input type="email" name="Email" id="email">
    <input type="password" name="Password" id="password">
    <input type="submit" value="Upload File" name="submit">
</form>

我还希望将图片存储到某个文件夹(如上传和路径)应该存储在数据库中。请帮我解答。谢谢您的回复

2 个答案:

答案 0 :(得分:2)

您可以使用nodejs formidable和fs-extra模块创建文件夹以在您的应用程序中存储图像。试试这个

var fs = require('fs-extra');
var formidable = require('formidable');
var EmpDetail   = require('../models/employee.model.js');//This line means that I am having my schema code.
var    util = require('util');


app.route('/upload').post(function(req,res){

res.json({success:true});

var empData = new EmpDetail();//Here I created object to call my schema values.

var form   =  new formidable.IncomingForm();
console.log(req.body,'req.body');
form.parse(req,function(err,fields,files){
    console.log('dfgbfbf');
    util.inspect({fields: fields, files: files});
});

form.on('field',function(name,value){//This means your html input fields
    console.log('fields',name,value);
    if(name == 'fullName'){
        console.log('here',value);
        empData.fullName = value;
    }
    if(name == 'fatherName'){
        empData.fatherName = value;
    }
    if(name == 'dt'){
        empData.DOB = value;
    }
});
form.on('file',function(field, file){//This means on click of file it gets that file.
    console.log(file.name,'hjgjg');
    var temp = file.path;
    console.log(temp,'temp',file.name);
    var fileName = file.name;
        var fileName = file.name;
        var location     = 'images/';
        var cpLocation   = 'empDetails/images/';
        empData.imgSize = file.size;
        empData.imgType = file.type;
        empData.picFile = location+fileName;//Here PicFile is name of Avatar in model
fs.copy(temp,cpLocation+fileName ,function(err){//This means it create a folder and name of the image in your app.
        if(err)
        {
            console.log(err);
        }
    });

});
form.on('end',function(fields, files){

        empData.save(function(err,resobj){
            if(err)
            {    
               throw err;
            }
        });
});
});

答案 1 :(得分:0)

有一个名为skipper的小节点模块可以处理文件上传。使用以下命令安装:npm install skipper --save。由于您使用express,您的主文件应如下所示:

var express = require('express');
var app = express();

app.use(require('skipper')());

app.post('/upload', function (req, res) {
  req.file('avatar').upload(function (err, uploadedFiles) {

  if (err) return res.send(500, err);
    return res.json({
      message: uploadedFiles.length + ' file(s) uploaded successfully!',
      files: uploadedFiles
    });
  });
});

您的html表单应该类似于:

<form action="/upload" method="post" enctype="multipart/form-data">
    Select file to upload:
    <input type="file" name="avatar" id="fileToUpload">
    <input type="submit" value="Upload File" name="submit">
</form>

注意:此路由将返回json对象。