Я пытаюсь отправить изображение на сервер, но сервер не может получить это изображение, я не знаю, почему это происходит.
Я сделал все настройки, используя мультер, но он не работает, я не знаю, что я делаю неправильно, они могут помочь?
ОШИБКА: файл не определен.
Service.js
const express = require('express');
const consign = require('consign');
const bodyParser = require('body-parser');
const expressValidator = require('express-validator');
const session = require('express-session');
const paginate = require('express-paginate');
const cookieParser = require('cookie-parser');
const multer = require('multer');
// const upload = multer({ dest: 'app/images/' });
const upload = multer({ dest: 'uploads/' });
const app = express();
app.set('view engine', 'ejs');
app.set('views', './app/views');
app.use(express.static('./app/public'));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(upload.single('fileUploadPlease'));
// keep this before all routes that will use pagination
app.use(paginate.middleware(10, 50));
app.use(cookieParser());
app.use(expressValidator());
app.use(session({
secret: 'asdfasjfasdf',
resave: false,
saveUninitialized: true,
cookie: { expires: new Date(253402300000000) },
}));
consign()
.include('app/routes')
.then('config/dbConnection.js')
.then('app/controllers')
// .then('app/models')
.into(app);
module.exports = app;
1010 * маршруты *
module.exports = (application) => {
application.get('/', (req, res) => {
});
application.post('/search', (req, res) => {
console.log(`Here is the file image: ${JSON.stringify(req.file)}`);
});
};
HTML
<!DOCTYPE HTML>
<html lang="pt-br">
<head>
<meta charset="UTF-8"/>
<title>Document</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</head>
<body>
<div>
<form class ="testeMongoose" action= "/" method= "get">
</form>
<div id="contain" class="container">
<div class="col-md-12">
<div class="card border-default mb-3">
<form class ="testeMongoose" action= "/search" method= "post">
<div class="fileUpload btn btn-primary col-md-4 mb-2">
<span>Carrega arquivo xlsx</span>
<input name="fileUploadPlease" id="fileUploadPlease" type="file" class="upload" />
</div>
<div class="form-group ">
<div class="col-sm-10">
<button type="submit" class="btn btn-primary" id="Import">Enviar</button>
</div>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
Возвращение req.file = undefined. Я не понимаю, почему ты не прав.