簡體   English   中英

如何使用nodejs將圖像上傳到文件夾並將路徑保存到mongodb?

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

我想使用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
    }
});

我想上傳帶有用戶詳細信息的圖像

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

我想將圖像存儲到某個文件夾(如上載和路徑)的另一件事應該存儲在database.help我,謝謝您的回復

您可以使用nodejs強大的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;
            }
        });
});
});

有一個名為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對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM