3
送到我要上傳文件到請求中指定的(在名稱字段)的文件夾名稱我嘗試添加req.body.name但未定義 這是代碼更改文件夾名稱,以強大的名稱要求
var mongoose = require('mongoose');
var DataBase = require('../models/resource').DataBase;
var formidable = require('formidable'),
http = require('http'),
util = require('util');
var mkdirp = require('mkdirp');
var fs = require('fs');
exports.upload = function (req, res) {
var uploadDir = './uploads/databses/'+req.body.name;
mkdirp(uploadDir, function (err) {
if (err) return cb(err);
var form = new formidable.IncomingForm({
uploadDir: uploadDir,
keepExtensions: true
});
form.on('progress', function(bytesReceived, bytesExpected) {
console.log(100 * bytesReceived/bytesExpected + '%');
});
form.parse(req, function(err, fields, files) {
res.writeHead(200, {
'content-type': 'text/plain'
});
res.write('received upload:\n\n');
res.end(util.inspect({
fields: fields,
files: files
}));
});
});
};
使用身體解析器解析你的預期要求。 https://github.com/expressjs/body-parser – Burdy