我一直试图保存表单字段与图像一起,棘手的部分是,我的形式具有动态输入创建arrays
。
我尝试使用Formidable,但是图像无法上传
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
mongooseUniqueValidator = require('mongoose-unique-validator');
var schema = new Schema({
sales_id : { type: Schema.Types.ObjectId, ref: 'SalesModel' },
salesItems : { type: [] }
},{
timestamps: true
});
schema.plugin(mongooseUniqueValidator);
module.exports = mongoose.model('jobs', schema);
我的表格很长,但这是值得关注的部分。
var express = require('express'),
router = express.Router(),
jwt = require('jsonwebtoken'),
mongoose = require('mongoose'),
mkdirp = require('mkdirp'),
multer = require('multer'),
IncomingForm = require('formidable').IncomingForm;
/*
* ==================================================
* Post the Sales information capture
* ==================================================
*/
router.post('/', function (req, res, next) {
var decoded = jwt.decode( req.query.token ),
datYear = new Date().getFullYear().toString(),
storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, mkdirp.sync( './uploads/' + datYear ) );
},
filename: function (req, file, cb) {
cb(null, Date.now()+'.'+file.originalname);
}
}),
Upload = multer({storage: storage}).any();
User.findById( decoded.user._id, function (err, user) {
if (err) { return res.status(500).json({ title : "Not Authorised !44", error : err }); }
Upload(req ,res ,function ( err ) {
if(err){
return res.status(501).json({error:err});
}
var dataCapture = new Sales({
user: user,
client_name : req.body.client_name,
client_contact_person : req.body.client_contact_person,
client_contact_email : req.body.client_contact_email,
client_contact_number : req.body.client_contact_number,
co_order_no : req.body.co_order_no,
client_code : req.body.client_code,
co_designer : req.body.co_designer,
}),
lineItems = new Jobs({
sales_id : dataCapture,
salesItems : req.body.itemRows,
in_design : false,
que_to_design : false
}),
imageNames = new Images({});
var derivatives = req.body.itemRows;
for( i= 0; i < derivatives.length; i++){
imageNames.sales_id = dataCapture;
imageNames.item_photo = derivatives[i].item_photo.replace(/\s/g, '').toLowerCase();
imageNames.item_measurements = derivatives[i].item_measurements.replace(/\s/g, '').toLowerCase();
imageNames.item_client_ci = derivatives[i].item_client_ci.replace(/\s/g, '').toLowerCase();
imageNames.item_artwork = derivatives[i].item_artwork.replace(/\s/g, '').toLowerCase();
imageNames.item_quotation = derivatives[i].item_photo.replace(/\s/g, '').toLowerCase();
}
dataCapture.save(function (err, result) {
if (err) {
return res.status(500).json({
title: 'An error occurred Saving Sale data',
error: err
});
}
user.save();
});
imageNames.save( function (err, result) {
if (err) {
return res.status(500).json({
title: 'An error occurred Saving Images data',
error: err
});
}
});
lineItems.save( function (err, result) {
if (err) {
return res.status(500).json({
title: 'An error occurred while trying to save Sub Jobs',
error: err
});
}
});
return res.status(201).json({
message: 'Data Captured Successfully !',
originalname : req.file
});
});
});
});
我想将图像保存在上载文件夹中,并记录在数据库中上载的图像,应该是两个条目。
我的上传目录为空