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

后端 未结 8 1109
[愿得一人]
[愿得一人] 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 22:55

    Here is a solution that works for me using multiparty.

    The answer given by jfizz does not work on all browsers.

    VERY IMPORTANT: As josh3736 mentioned on a comment above, you MUST set the Connection header to close!

    form.on('part', function(part) {
      if (![some condition I want to be met]) {
        form.emit('error', new Error('File upload canceled by the server.'));
        return;
      }
      ... code to handle the uploading process normally ...
    });
    
    form.on('error', function(err) {
      console.log('ERROR uploading the file:',err.message);
      res.header('Connection', 'close');
      res.status(400).send({ status:'error' });
      setTimeout(function() { res.end(); }, 500);
      next(err);
    });
    

提交回复
热议问题