我正在使用ExpressJS和NodeJS。 我建立了一个注册页面,要求用户上传他的图像。 Jade中的代码
.form-container
h1 Create a User
form(name="adduser",method="post",action="/adduser")
.col-xs-13
p Username
input.form-control(type="text", name="username")
.col-xs-13
p Email
input.form-control(type="text", name="useremail")
.col-xs-13
p Upload an Image
input.form-control(type='file', name ="pic", accept='image/*')
.col-xs-13
p Password
input#password.form-control(type='text', name='pass', required='')
.col-xs-13
p Confirm Password
input#confirm_password.form-control(type='text', required='' , onkeyup='checkPass(); return false;')
span#confirmMessage.confirmMessage
br
.form-group
button#myBtn.btn.btn-primary(type="submit") submit
我也有一个脚本,但它现在无关紧要。 问题是我想将图像保存在我的Express Project目录中,但我不知道该怎么做。这里是将其他信息存储在Mongodb DB中的代码,我现在只需要保存图像。
/* POST to Add User Service */
router.post('/adduser', function(req, res) {
// Set our internal DB variable
var db = req.db;
// Get our form values. These rely on the "name" attributes
var userName = req.body.username;
var userEmail = req.body.useremail;
var userPass = req.body.pass;
var collection = db.get('users');
// Submit to the DB
collection.insert({
"username" : userName,
"email" : userEmail,
"password" : userPass
}, function (err, doc) {
if (err) {
res.send("There was a problem adding the information to the database.");
}
else {
res.redirect("userlist");
}
});
});
答案 0 :(得分:0)
这可能会对您有所帮助:
var express = require('express'),
app = express(),
formidable = require('formidable'),
util = require('util'),
fs = require('fs-extra'),
bodyparser=require('body-parser'),
qt = require('quickthumb'),
path = require('path');
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/db');
var Images = require('./model.js');
app.use(qt.static(__dirname + '/'));
app.use(bodyparser());
app.set('view engine','ejs');
app.post('/upload',function (req, res){
var form = new formidable.IncomingForm();
var userObj = [];
form.parse(req, function(err, fields, files) {
});
form.on('field',function(name,value){
// you will get the all the input field values here...
console.log(name,value);//name gives input field name and value gives input value
userObj.push({name:value});
});
form.on('end', function(fields, files) {
//You can save the data into collection from the array
//Images is my model
var img = new Images();
var temp_path = this.openedFiles[x].path;
/* The file name of the uploaded file */
var file_name = this.openedFiles[x].name;
//console.log('file '+file_name);
img.size = this.openedFiles[x].size;
img.type = this.openedFiles[x].type;
/* Location where we want to copy the uploaded file */
var new_location = 'uploads/';
//to copy the file into a folder
fs.copy(temp_path, new_location + file_name, function(err) {
if (err) {
console.log(err);
}
});//fscopy
});//form end
res.send('Done!!');
});//post
app.listen(3000);
console.log('started server');
答案 1 :(得分:0)
用于快递。
var formidable = require('formidable');
我很厉害
{'1 and 2': 524, '2 and 5': 820, '2 and 6': 1, '1 and 3': 332, '1 and 4': 77, '3 and 4': 395, '3 and 5': 823}