上传文件服务端
exports.postFile = function (req, res) {
var upfile = req.files.upfile;
var files = [];
if (upfile instanceof Array) {
files = upfile;
} else {
files.push(upfile);
}
for (var i = 0; i < files.length; i++) {
var file = files[i];
var path = file.path;
var name = file.name;
var target_path = "./upload/" + name;
fs.rename(path, target_path, function (err) {
if (err) throw err;
});
}
res.render('index', { title:'Complete' });
}
另外app.js加个app.use(express.bodyParser({uploadDir:'./upload_tmp'}));
页面前端
<form method="post" action="/upload" enctype="multipart/form-data">
<input type="file" name="upfile" multiple/>
<input type="submit" value="upload">
</form>
8 回复
觉得那res.render 来得早了点,我会这样的,不过没试过:
fs.rename(path, target_path, function (err) {
if (err) throw err;
if (i == files.length)
res.render('index', { title:'Complete' });
});