Search code examples
javascriptnode.jsmulter

Page goes in a buffer state after file uploads using multer?


I am uploading a file to a temp directory and after I click submit to upload my file, my page stays in a buffering state. I'm bit of a noob to node so any help is greatly appreciated. I'll add, the file uploads fine and the web app works fine while its in the "buffer state" its just a little annoying. It gives an open tio press esc to cancle or click to stop it. My code is as follows

<div class="button-wrap">
   <form action="/public/index.html" enctype="multipart/form-data" 
   method="POST">
   <label class="button" for="upload">Upload File</label>
   <input id="upload" name="filetoupload" type="file">
</div>
var fileStorage = multer.diskStorage({
destination: (req, file, cb) => {
    cb(null, './public/temp')
},
filename: (req, file, cb) => {
    cb(null, Date.now() + '--' +file.originalname)
}
});

var upload = multer({ storage: fileStorage })

app.post('/public/index.html', upload.single('filetoupload'), (req, res)=>{
console.log(req, fileStorage);
// res.send('File uploaded');
});

Solution

  • You forgot to actually send a response. If you don't need/want to send a response body, you can do e.g.:

    app.post('/public/index.html', upload.single('filetoupload'), (req, res)=>{
      res.status(204).end();
    });