How to cancel user upload in Formidable (Node.js)?

后端 未结 8 1085
[愿得一人]
[愿得一人] 2021-02-07 22:14

I have been working on this problem for two days now, and I am stuck. I am using Node.js with Express, and I am trying to implement an upload form. Basically, I want the form to

8条回答
  •  一向
    一向 (楼主)
    2021-02-07 23:01

    Here is my solution:

    var maxSize = 30 * 1024 * 1024;    //30MB
    app.post('/upload', function(req, res) {
    
        var size = req.headers['content-length'];
        if (size <= maxSize) {
            form.parse(req, function(err, fields, files) {
                console.log("File uploading");
                if (files && files.upload) {
                    res.status(200).json({fields: fields, files: files});
                    fs.renameSync(files.upload[0].path, uploadDir + files.upload[0].originalFilename);
                }
                else {
                  res.send("Not uploading");
                }
            });
        }
        else {
            res.send(413, "File to large");
        }
    

    And in case of wasting client's uploading time before get response, control it in the client javascript.

    if (fileElement.files[0].size > maxSize) {
        ....
    }
    

提交回复
热议问题