ninad kelkar
ninad kelkar

Reputation: 61

File Upload using node js without multer

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?

Upvotes: 6

Views: 5236

Answers (1)

rsp
rsp

Reputation: 111336

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.

Upvotes: 4

Related Questions