Search code examples
node.jsformspostformidable

Node.js - How do I limit the file upload size of a multipart form in formidable


I know formidable has a form.maxFieldsSize, but that doesn't include files. How would I limit the maximum file size uploaded to a multipart form in formidable, and terminate the form if the file size is too large?


Solution

  • I think you can use the code down below for that kind of task.

    var sizeLimitBytes = 2000;
    form.on('progress', function(bytesReceived, bytesExpected) {
      if(bytesReceived > sizeLimitBytes ){
        return false; //exit the program
      }
    });