N
N
NarkoMan012021-03-03 15:22:57
Express.js
NarkoMan01, 2021-03-03 15:22:57

Multer not saving file?

Good afternoon!
I can not understand why it accepts, but does not save the file on the server.

<form class="li-group-item-img-upload-form" action="/upload" method="post" enctype="multipart/form-data">
                   <label>Файл</label><br>
                   <input type="file" name="filedata" id="input-file-img"/><br><br>
                   <input type="submit" name="filedata" value="Отправить" id="send-img-submit"/>
                 </form>

backend
const multer  = require('multer')


app.use(multer({dest:"uploads"}).single("filedata"));
app.post("/upload",function (req, res, next) {
    
        let filedata = req.files.filedata;
    console.log(filedata)
        if(!filedata)
            res.send("Ошибка при загрузке файла");
        else
            res.send("Файл загружен");
    });

PS in the console I get:
{
  name: 'IMG_4746.JPG',
  data: <Buffer ff d8 ff e1 21 5a 45 78 69 66 00 00 4d 4d 00 2a 00 00 00 08 00 12 00 0b 00 02 00 00 00 26 00 00 00 e6 01 00 00 03 00 00 00 01 14 b5 00 00 01 01 00 03 ... 6295814 more bytes>,
  size: 6295864,
  encoding: '7bit',
  tempFilePath: '',
  truncated: false,
  mimetype: 'image/jpeg',
  md5: 'd7d51913789a68a841d7d596b796678d',
  mv: [Function: mv]
}


Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
NarkoMan01, 2021-03-03
@NarkoMan01

the problem was solved. Because data from the client comes in bits, then I had to manually write them through

fs.writeFile('testImg.jpg',filedata.data,()=>{
            console.log('ok')
        })

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question