0

I am trying to make a file upload using Node.js and the Formidable module.

npm install formidable

And then I made this, please read the notes - where I can explain what each function does and describes the algorithm:

// get access to the files that were sent;
// at this time I don't want the files to be uploaded yet;
// in the next function I will validate those files.
function form_parse() {
    form.parse(req, (err, fields, files) => {
      if (err) return req.Cast.error(err);
      if (Object.keys(files).length==0) return req.Cast.badRequest();
      req.files = files;
      return validate_files();
    });
  }

  // I made an object with options to validate against the
  // files. it works and continues to the process_files()
  // function only whether files are verified.
  function validate_files() {
    let limitations = require('../uploads-limitations');
    try {
      limitation = limitations[req.params.resource];
    } catch(err) {
      return req.Cast.error(err);
    }
    let validateFiles = require('../services/validate-files');
    validateFiles(req, limitation, err => {
      if (err) return req.Cast.badRequest(err);
      return process_files();
    });
  }

  // here is the problem - form.on doesn't get fired.
  // This is the time I want to save those files - after
  // fully verified
  function process_files() {
    form.on('file', function(name, file) {
      console.log(`file name: ${file.name}`);
      file.path = path.join(__dirname, '../tmp_uploads/' + file.name);
    });
    form.on('error', err => {
      return req.Cast.error(err);
    });
    form.on('end', () => {
      console.log(`successfully saved`);
      return req.Cast.ok();
    });
  }

  form_parse();

As you can see and as I have described - the validation works but when I want to actually save those files the form.on (events) are not getting fired.

Raz Buchnik
  • 7,753
  • 14
  • 53
  • 96

1 Answers1

2

Yes because at the end of your process, after parsing and validating, you attach events listeners. This should be done first, before starting parsing. Because these events (on file, on error, on end) happen during the parsing, not after.

 form.on('file',...) // First, attach your listeners
    .on('error', ...)
    .on('end', ...);

form.parse(req) // then start the parsing
Jeremy Thille
  • 26,047
  • 12
  • 43
  • 63
  • I don't get this. If I use on file I can make validations BEFORE files are actually getting uploaded? That means - the form.on('file') is just give me an instance of the file so I can run validations on it, but it not actually uploads (and receive the actual file data) the file, isn't it? – Raz Buchnik Jan 18 '19 at 10:59
  • `form.on` does not trigger anything. It does not execute anything. It creates event listeners. What it does is adding a little guy whose job is "Whenever the form has an error, or is finished, do something". Then the little guy waits and listens. _Then_ you start parsing your form, and events happen, and the little guy starts noticing events and doing things. Currently, you're adding the little guy _after_ everything is parsed and happened, so he listens, but nothing's happening. – Jeremy Thille Jan 18 '19 at 11:03
  • I am asking if I can verify file's extension and size before saving them in my server. And moreover, before the user fully waits for all of the files are actually transformed to the server (and makes the server working for nothing) – Raz Buchnik Jan 18 '19 at 11:13
  • @Raz On your first comment what you say is wrong. The file has already upload to the server, but is in the memory and not saved yet. Also check this out [How to cancel user upload in Formidable (Node.js)?](https://stackoverflow.com/questions/20553575/how-to-cancel-user-upload-in-formidable-node-js) – Stamos Jan 18 '19 at 11:51
  • 1
    @Raz What does this have to do with anything discussed before? File extension? What file extension? This sounds like an entirely different problem/question... – Jeremy Thille Jan 18 '19 at 12:15
  • Basically I want to allow, for example, 3 files where each file is 200kb and jpeg only (just for example). I understand, as Stamos said, that if I can verify the file that means that the file is already in the server. And therefore I want to know two things actually: 1. will the file be deleted automatically if I don't save it (using formidable). 2. what going to be happened if the user is going to upload 1gb file. The 1gb file will be hold in the temp directory - or that it will be rejected once it exceeded the 200kb? – Raz Buchnik Jan 18 '19 at 12:37
  • But... this is another question altogether. It has nothing to do with your original question of events not being triggered. I answered your first question. If you have another one, then create another question on Stackoverflow. – Jeremy Thille Jan 18 '19 at 13:17
  • @JeremyThille you are right, those are different questions that were led from the the first one. – Raz Buchnik Jan 18 '19 at 13:34
  • OK but was the first one solved? Let's finish the first one before tackling another. Because you never said if the first problem was solved or not – Jeremy Thille Jan 18 '19 at 13:43