+upload?
This commit is contained in:
42
app.js
42
app.js
@@ -4,9 +4,31 @@ const fs = require("fs");
|
|||||||
const { join } = require("path");
|
const { join } = require("path");
|
||||||
const { log } = require("console");
|
const { log } = require("console");
|
||||||
|
|
||||||
|
// express app
|
||||||
const app = express();
|
const app = express();
|
||||||
const jsonParser = bodyParser.json();
|
const jsonParser = bodyParser.json();
|
||||||
|
|
||||||
|
// multer
|
||||||
|
const multer = require("multer");
|
||||||
|
const { PassThrough } = require("stream");
|
||||||
|
const upload = multer();
|
||||||
|
const imageFolder = join(__dirname, "images");
|
||||||
|
fs.mkdirSync(imageFolder, { recursive: true }); // Создать папку, если не существует
|
||||||
|
const imageStore = multer.diskStorage({
|
||||||
|
destination: function (_req, file, cb) {
|
||||||
|
console.log("file : ", file);
|
||||||
|
return cb(null, imageFolder);
|
||||||
|
},
|
||||||
|
filename: function (_req, file, cb) {
|
||||||
|
console.log("file from filename function : ", file);
|
||||||
|
let origFileName = file.originalname.split(".")[0];
|
||||||
|
let filename =
|
||||||
|
origFileName + "-" + Date.now() + path.extname(file.originalname);
|
||||||
|
console.log("filename : ", filename);
|
||||||
|
return cb(null, filename);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
// local bootstrap
|
// local bootstrap
|
||||||
app.use(
|
app.use(
|
||||||
"/bootstrap",
|
"/bootstrap",
|
||||||
@@ -137,6 +159,26 @@ app.put("/api/users", jsonParser, function (req, res) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// multer
|
||||||
|
// Получение изображений пользователей
|
||||||
|
// app.get("/api/images", load, function (req, res) {
|
||||||
|
// res.status(200).send();
|
||||||
|
// });
|
||||||
|
|
||||||
|
// Загрузка изображения для пользователя
|
||||||
|
app.post("/api/images/upload/:id", upload.single("file"), (req, res) => {
|
||||||
|
if (!req.body) return res.sendStatus(400);
|
||||||
|
if (!req.body.name) return res.sendStatus(400);
|
||||||
|
|
||||||
|
var userId = req.body.id;
|
||||||
|
|
||||||
|
console.log("Загружается файл", userId);
|
||||||
|
fs.unlinkSync(req.file.path);
|
||||||
|
res.status(200).send();
|
||||||
|
});
|
||||||
|
|
||||||
|
// Удаление изображения для пользователя
|
||||||
|
|
||||||
app.listen(3000, function () {
|
app.listen(3000, function () {
|
||||||
log("Сервер на порту 3000");
|
log("Сервер на порту 3000");
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -39,6 +39,7 @@
|
|||||||
<th>Имя</th>
|
<th>Имя</th>
|
||||||
<th>Возраст</th>
|
<th>Возраст</th>
|
||||||
<th>Опции</th>
|
<th>Опции</th>
|
||||||
|
<th>Изображение</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody></tbody>
|
<tbody></tbody>
|
||||||
|
|||||||
Reference in New Issue
Block a user