私のプロジェクトには、ファイルをロードする関数があり、少し役立つかもしれません:
var app = express.createServer(
express.bodyParser({uploadDir: "public/files", keepExtensions: true})
, express.cookieParser()
, express.session({ secret: 'keyboard cat' })
);
app.post('/upload', function (req, res) {
var msg = '';
var img = '';
//console.log("type: "+req.files.image.type);
//console.log("size: "+req.files.image.size);
if(req.files.image.type != 'image/png' && req.files.image.type != 'image/jpeg' && req.files.image.type != 'image/gif')
{
msg = 'Invalid format, accepts only: jpg, png and gif.<br/>';
}
if(req.files.image.size > 307200) // 300 * 1024
{
msg += 'File size no accepted. Máx: 300kb.<br/>';
}
if(msg == '')
{
if(diff > 0)
{
name = name.substring(name.length-diff, name.length);
}
var date = new Date();
var name = req.files.image.name;
var diff = name.length - 20;
var rnd_number = Math.floor(Math.random()*101);
var new_name = date.format('yyyymmdd_HHMMssl_') + rnd_number +'_'+ name;
fs.renameSync(req.files.image.path, 'public/files/img'+new_name);
img = '<img src="public/files/img/'+new_name+'" width="100%"/>';
}
res.render('admin/upload', {layout: false, img: img, msg: msg});
})