TypeError: Cannot read the 'image' property from undefined

Something is wrong in my source code, but I canโ€™t understand that - please help. I was looking for some solutions that some found and updated the source code for them, but did not help.

var express = require('express'); var fs = require('fs'); var bodyParser = require('body-parser'); var app = express() app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: false })); var form = "<!DOCTYPE HTML><html><body>" + "<form method='post' action='/upload' enctype='multipart/form-data'>" + "<input type='file' name='image' id='image'/>" + "<input type='submit' /></form>" + "</body></html>"; app.get('/', function(req, res){ res.writeHead(200, {'Content-Type': 'text/html'}); res.end(form); }); app.post('/upload', function(req, res){ fs.readFile(req.files.image.path, function(err, data){ var imageName = req.files.image.name if(!imageName){ console.log("There was an error"); res.redirect('/'); res.end(); }else{ var newPath = __dirname + "/uploads/fullsize/" + imageName; fs.writeFile(newPath, data, function(err){ res.redirect("/uploads/fullsize/" + imageName); }); } }); }); app.listen(8080); 
+2
source share
1 answer

The body-parser tool does not process multipart bodies.

From body-parser github:

 This does not handle multipart bodies, due to their complex and typically large nature. For multipart bodies, you may be interested in the following modules: 

https://github.com/expressjs/body-parser

+4
source

Source: https://habr.com/ru/post/1499302/


All Articles