@aleshaykovlev
html, css, js, node, webpack, sass, react

Ошибка при загрузке файла?

Ошибка:

MulterError: Unexpected field...


multer в server.js:
const multer = require('multer');
const upload = multer({ dest: 'upload/' });
const type = upload.single('filedata');

app.post('/profile/:id/user-img', type, async(req, res) => {
	try {
		const {img} = req.body;
		const userFind = await User.findById(req.params.id);

		if (!img) {
			console.log('Ошибка при загрузке файла');
		} else {
			console.log('Файл загружен');
			
			await User.updateOne({
				img: img
			})

			res.redirect('/profile/'+userFind);
		}
	} catch (e) {
		console.log(e.message);
	}
})


multer в html (pug):
form(action="/profile/"+ user._id +"/user-img" method="POST" enctype="multipart/form-data")
        input(type="file" name="img" required)
        input(type="submit" value="Load")
  • Вопрос задан
  • 367 просмотров
Решения вопроса 1
notiv-nt
@notiv-nt
Как ваше ничего? Да, моё тоже
Открываем документацию
читаем
Accept a single file with the name fieldname. The single file will be stored in req.file.


проверяем
single file with the name fieldname

input(type="file" name="img" required)


-_-

single('filedata')

name="img"
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы