简体   繁体   English

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

[英]how to upload the image to folder using nodejs and save the path into mongodb?

i want to upload image using angularjs or html5,Express.jsjs and mongodb.i have followed tutorials i can't able to upload the image to upload folder please some one help me how to do that.below i have given my schema code 我想使用angularjs或html5,Express.jsjs和mongodb.image上传图片。我按照教程进行操作,但是我无法将图片上传到文件夹,请有人帮我做。下面给出了我的架构代码

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

i want to upload the image with user details 我想上传带有用户详细信息的图像

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

one more thing i want to store image to some folder like upload and path should be store in database.help me out .thanks for your response 我想将图像存储到某个文件夹(如上载和路径)的另一件事应该存储在database.help我,谢谢您的回复

You can use nodejs formidable and fs-extra module for creating folder to store image in your application. 您可以使用nodejs强大的fs-extra模块创建用于在应用程序中存储图像的文件夹。 Try this 尝试这个

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

There is a small node module called skipper which can handle file uploads. 有一个名为skipper的小型节点模块,可以处理文件上传。 Install it with: npm install skipper --save . 使用以下命令安装它: npm install skipper --save Since you are using express your main file should look something like this: 由于您正在使用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
    });
  });
});

And your html form should look something like: 而且您的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>

Note: This route will return json object. 注意:此路由将返回json对象。

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

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