当我开始使用我的应用程序上传文件时,它会在第一步后存货。 上传功能的Bellow代码
exports.upload = function(req, res){
var form = formidable.IncomingForm();
console.log('Upload');
form.parse(req, function(err, fields, files){
if(err){
console.log(err);
res.send(err);
return;
}
console.log('Parsing');
fs.readFile(files.image.path, function(err, data){
if(err){
res.send(err);
}
var imageName = files.image.name;
if(!imageName){
console.log("Parsing issue - sending 500");
res.writeHead(500, {'Content-type':'text/plain'});
res.write('Parsing error');
res.end();
return;
}
var fullPath = photoDir + imageName;
var thumbPath = thumbDir + imageName;
fs.writeFile(fullPath, data, function(err){
im.resize({
srcPath: fullPath,
dstPath: thumbPath,
width: 100
},function(err, stdout, stderr){
if(err){
console.log(err);
res.send(err);
return;
}
var newPhoto = new Photo({
'fullsize_name':imageName,
'thumbsize_name':imageName
});
newPhoto.save(function(err){
if(err){
res.send(err);
}
res.json(newPhoto);
console.log(newPhoto);
});
});
});
});
});
}
服务器配置部分
var express = require('express');
var logger = require('morgan');
var body_parser = require('body-parser');
var mongoose = require('mongoose');
var passport = require('passport');
var multipart = require('connect-multiparty');
.
.
.
var app = express();
var router = express.Router();
var multipartMiddleware = multipart();
.
.
.
/** PHOTO OPERATIONS **/
router.route('/photos')
.post(authController.isAuthenticated, photoController.upload);
.
.
.
app.use(passport.initialize());
app.use(logger('dev'));
app.use(multipartMiddleware);
app.use(body_parser.json());
app.use(body_parser.urlencoded({
extended: true
}));
app.use('/api', router);
当我打电话
时,你知道为什么它会停止吗?form.parse(req,function(err,fields,files){
答案 0 :(得分:1)
您是否在html文件中错过了表单标记中的{enctype ='multipart / form-data'}?添加它可能会有效。