0

I read this tutuorial and tried it:
https://debugmode.net/2014/01/14/how-to-upload-file-in-node-js/

html:

<form action="/post-a-file" method="post" enctype="multipart/form-data" >
<input type="file" name="file" required>
</form>

app.js:

app.post("/post-a-file",  function (req, res){

    var destinationFile = fs.createWriteStream(__dirname + "/movies");
    req.pipe(destinationFile)
    
   req.on('end', ()=>{ console.log("end") })
})

but there is not a file in /movies.

bahoz99
  • 121
  • 1
  • 6

1 Answers1

0

You cannot do it this way. Your post is arriving as multipart/form-data and you need middleware to parse that for you. That middleware will then likely give you an option to put the resulting file data into a file. See multer for one option for that middleware. If you're just uploading a single file, you probably want to use the upload.single() variation of multer.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • can I make a procces bar with multer? (X% completed...) – bahoz99 Mar 29 '21 at 23:46
  • @bahoz99 - It's worth doing a search on that topic. Here's one of the search results: [Tracking progress of file upload in multer with nodejs](https://stackoverflow.com/questions/35288625/tracking-progress-of-file-upload-in-multer-nodejs). There are other posts on the same topic. – jfriend00 Mar 30 '21 at 02:06