简体   繁体   English

Express.js和multer:如何知道文件何时全部上传?

[英]Express.js and multer: how to know when the files are all uploaded?

I'm using Multer module for file uploads. 我正在使用Multer模块上传文件。 While it all works ok, there's a warning at the end of their github page, which reads: "WARNING: req.body is fully parsed after file uploads have finished. Accessing req.body prematurely may cause errors." 虽然一切正常,但在github页面末尾有一个警告,上面写着:“警告:文件上传完成后req.body被完全解析。过早访问req.body可能会导致错误。”

This has got me really worried. 这让我非常担心。 I just can't find a way to let the .post middleware know when the file(s) have been uploaded and req.body is ready to use. 我找不到让.post中间件知道何时上传文件并且req.body准备好使用的方法。 Here's my code: 这是我的代码:

app.js: app.js:

app.use(multer({ 
        dest: './uploads/',
        rename: function (fieldname, filename) {
            return filename.replace(/\W+/g, '-').toLowerCase() + Date.now();
        },
        putSingleFilesInArray: true       
        })
);

upload.js: upload.js:

router.route('/')
    .get(function(req, res){
        res.render('uploads');
    })
    .post(function(req, res){
        //how to wait here for the file to upload?
    });

While I am aware of onParseEnd, but I don't know how to implement it, so that I have at least some kind of information about the upload process being finished. 虽然我知道onParseEnd,但我不知道如何实现它,所以我至少有一些关于上传过程的信息。

Multer is part of the router chain. Multer是路由器链的一部分。 This means that express will execute multer first, and only once multer has completed parsing the form it will continue execution to your .post() handler. 这意味着express将首先执行multer,并且只有multer完成解析表单后才会继续执行.post()处理程序。 The warning on the page is meant for accessing req.body from multer callbacks, like onFileUploadData() and similar. 页面上的警告用于从multer回调访问req.body ,如onFileUploadData()和类似的。 Therefore, the order of execution is: 因此,执行顺序是:

  • onParseStart onParseStart
  • onFileUploadStart/onFileUploadData... onFileUploadStart / onFileUploadData ...
  • onFileUploadComplete onFileUploadComplete
  • onParseEnd onParseEnd
  • your .post() handler 你的.post()处理程序

If I understand the documentation correctly the warning is only applicable to the event handlers you can pass to multer itself. 如果我正确理解文档,则警告仅适用于您可以传递给multer本身的事件处理程序。 When the request reaches your handler multer is already done and all files are already uploaded. 当请求到达您的处理程序时,multer已经完成,并且所有文件都已上传。

The problem exists for example in the rename event which you already use, but this function actually receives four arguments fieldname, filename, req, res . 例如,在您已经使用的rename事件中存在该问题,但此函数实际上接收四个参数fieldname, filename, req, res That means you have access to the request before it is fully parsed. 这意味着您可以在完全解析之前访问该请求。

截至2018年,onFileUploadComplete(file,req,res)不再是multer的一部分。

You can know it easily, there is option available in multer called 你可以很容易地知道它,multer中有一个选项可用

  • onFileUploadComplete(file, req, res) onFileUploadComplete(file,req,res)

You can use this and send response from here. 您可以使用此功能并从此处发送响应。

https://github.com/expressjs/multer#options https://github.com/expressjs/multer#options

    app.use(multer({
    dest: path.join(__dirname, '../uploads/fullsize'),
    rename: function (fieldname, filename) {
        return filename.replace(/\W+/g, '-').toLowerCase();
    },
    onFileUploadStart: function (file) {
        console.log(file.name + ' is starting ...');
    },
    onFileUploadComplete: function (file, req, res) {
        console.log(file.name + ' uploading is ended ...');
        console.log("File name : "+ file.name +"\n"+ "FilePath: "+ file.path)
    },
    onError: function (error, next) {
        console.log("File uploading error: => "+error)
        next(error)
    }
    onFileSizeLimit: function (file) {
        console.log('Failed: ', file.originalname +" in path: "+file.path)
        fs.unlink(path.join(__dirname, '../tmpUploads/') + file.path) // delete the partially written file
    }

}));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM