6

Just want simple file upload functionality . I have used fs-path that serves my purpose of creating dynamic folder structure and file at upload location. I am not able to achieve streaming of request file, that will have to be uploaded. My code is as follows :

fsPath.writeFile(path, **req.body**, function (err) {
    if (err) {
      throw err;
    } else {
      console.log('File Upload successful...');
      res.send({ code: 200, message: `File Upload successful` });
    }
  });

Need some insights about, how can I send request file as input in the above code snippet. How do I steam my request file, that will be written in respective upload location?

1 Answer 1

4

If you want to stream the request body then instead of using a body parser or multr you should use the req stream directly. Remember that the request object is a stream and you can use it as such:

    req.on('data', data => {
      console.log(data);
    });

You can also pipe it to some other stream, like a writable stream created with fs.createWriteStream etc.

Sign up to request clarification or add additional context in comments.

Comments

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.