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.

3
  • 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. Commented Jan 18, 2019 at 10:44
  • How can I manage this to be working? Commented Jan 18, 2019 at 10:45
  • I have added a full answer. Commented Jan 18, 2019 at 10:46

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

9 Comments

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?
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.
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 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)?
@Raz What does this have to do with anything discussed before? File extension? What file extension? This sounds like an entirely different problem/question...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.