如何使用nodejs将图像上传到文件夹并将路径保存到mongodb? [英] how to upload the image to folder using nodejs and save the path into mongodb?
问题描述
我想使用angularjs或html5上传图片,Express.jsjs和mongodb.i已经按照教程我无法将图片上传到上传文件夹请一些人帮我怎么做。我已经给了我模式代码
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
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
推荐答案
您可以使用nodejs formidable和fs-extra模块来创建用于存储图像的文件夹你的申请。试试这个
You can use nodejs formidable and fs-extra module for creating folder to store image in your application.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;
}
});
});
});
这篇关于如何使用nodejs将图像上传到文件夹并将路径保存到mongodb?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!