Gitlab@Informatics

Skip to content
Snippets Groups Projects
Commit 69aa9e9e authored by 65160117's avatar 65160117
Browse files

Edit server.js

parent fefe87d8
No related branches found
No related tags found
No related merge requests found
...@@ -18,16 +18,22 @@ app.use(express.static(path.join(__dirname, 'public'))); ...@@ -18,16 +18,22 @@ app.use(express.static(path.join(__dirname, 'public')));
// กำหนดการจัดเก็บไฟล์ // กำหนดการจัดเก็บไฟล์
const storage = multer.diskStorage({ const storage = multer.diskStorage({
destination: function (req, file, cb) { destination: function (req, file, cb) {
const uploadDir = 'public/uploads'; const uploadDir = path.join(__dirname, 'public/uploads');
// สร้างโฟลเดอร์ถ้ายังไม่มี // สร้างโฟลเดอร์ถ้ายังไม่มี
if (!fs.existsSync(uploadDir)){ if (!fs.existsSync(uploadDir)){
try {
fs.mkdirSync(uploadDir, { recursive: true }); fs.mkdirSync(uploadDir, { recursive: true });
} catch (err) {
console.error('Error creating upload directory:', err);
return cb(err);
}
} }
cb(null, uploadDir); cb(null, uploadDir);
}, },
filename: function (req, file, cb) { filename: function (req, file, cb) {
// สร้างชื่อไฟล์ใหม่เพื่อป้องกันการซ้ำ // สร้างชื่อไฟล์ใหม่เพื่อป้องกันการซ้ำ
cb(null, Date.now() + path.extname(file.originalname)); const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9);
cb(null, uniqueSuffix + path.extname(file.originalname));
} }
}); });
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment