Pierre Capo
Pierre Capo

Reputation: 1053

Nodejs handling uploaded files

I made a web app where people upload their files with drag and drop. They make a post a request to send their file to the nodejs server.

  uploadLocalFilesToDrive(e) {
    var files = e.dataTransfer.files;
    axios.post('/api/uploadLocalFilesToDrive', files[0])
    .then(function (res) {
       console.log('upload done');
     })
}

The goal of the nodejs server now, is to save this file on the server hard disk. This is what I made to handle the above request :

  uploadLocalFilesToDrive: (req, res) => {
    for(var key in req.body){
      filesystem.writeFile('new file name', key, function (err) {
        console.log("It's saved"); 
      });
    }
  }

It seems to work well with text files, but whenever the user upload a .pdf for instance, then the pdf becomes corrupted. I have a feeling it's an issue because the axios post request sends only text to the nodejs server, but I'm not sure. Help would be appreciated !

Upvotes: 0

Views: 123

Answers (1)

Rashad Ibrahimov
Rashad Ibrahimov

Reputation: 3309

Multer is the best solution for nodejs-expressjs file uploads. Try it

Upvotes: 1

Related Questions