How to do upload with express in node.js

2019-01-22 16:36发布

My code is like this:

app.configure(function () {
    app.use(express.static(__dirname + "/media"));
    app.use(express.bodyParser({
          keepExtensions: true
    }));
})

app.post('/upload', function (req, res) {
    console.log(req.files);

    res.send("well done");
    return;
})

ant do some job like:

1.Do someting on the progoress event, how can I bind handler to the progress, complete event, I have tried req.files.on('progress', fn), but it doesn't work

2 I know how to use req.files to get the file's imformation, but how can I limit the upload file's size before it upload, or limit the upload image resolution?

2条回答
乱世女痞
2楼-- · 2019-01-22 17:04

You should look at multipart middleware documentation, this is the one involved in file uploading.

It says that the limit is set via the "limit" option and that progress could be listened to if you put "defer" option to true. In that case the form used by the upload is set as an attribute of your request. Then you will be able to listen to the progress event.

So your code should look like this (not tested yet):

app.configure(function () {
    app.use(express.static(__dirname + "/media"));
    app.use(express.bodyParser({
          keepExtensions: true,
          limit: 10000000, // 10M limit
          defer: true              
    }));
})

app.post('/upload', function (req, res) {
    req.form.on('progress', function(bytesReceived, bytesExpected) {
        console.log(((bytesReceived / bytesExpected)*100) + "% uploaded");
    });
    req.form.on('end', function() {
        console.log(req.files);
        res.send("well done");
    });
})
查看更多
smile是对你的礼貌
3楼-- · 2019-01-22 17:15

I have a function in my project that loads files, might help you a bit:

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});

})
查看更多
登录 后发表回答