Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File Upload using node js without multer

Tags:

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?

like image 699
ninad kelkar Avatar asked Mar 15 '17 12:03

ninad kelkar


1 Answers

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.

like image 79
rsp Avatar answered Sep 25 '22 10:09

rsp