W
W
woodyJS2019-02-15 06:46:39
JavaScript
woodyJS, 2019-02-15 06:46:39

Why can't I read the file loaded from the form?

Good afternoon.
Why can't read the file uploaded from the form?
Always throws an error : Cannot read property 'buffer' of undefined
form (PUG):

form(action='/', method='POST' , enctype="form-data")
    p
      input(type='file', name='down', value='')
    input(type='submit',name="downloaded_file", value='загрузить')

1) option
...
var multer = require('multer')
var upload = multer({ dest: 'uploads/' })
...

app.post('/', upload.single('down'), function (req, res, next) {
    var raw = new Buffer(req.file.buffer.toString(), 'utf-8');

    fs.writeFile('./uploads/test1.txt', raw, function (err) {
        if (err) {
            console.log('Error');
            return next(err);
        }
        res.end('Success!');
    });
});

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly Yakovenko, 2019-02-15
@Xazzzi

Try enctype='multipart/form-data'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question