Gitlab@Informatics
Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
W
webposts
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
65160258
webposts
Commits
d6490c2d
Commit
d6490c2d
authored
2 months ago
by
65160258
Browse files
Options
Downloads
Patches
Plain Diff
del Multer
parent
b6310369
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
routes/posts.js
+24
-23
24 additions, 23 deletions
routes/posts.js
with
24 additions
and
23 deletions
routes/posts.js
+
24
−
23
View file @
d6490c2d
...
...
@@ -145,29 +145,30 @@ router.get('/:id', async (req, res) => {
});
// ตั้งค่า Storage สำหรับ Multer
const
storage
=
multer
.
diskStorage
({
destination
:
(
req
,
file
,
cb
)
=>
{
cb
(
null
,
'
public/uploads/
'
);
// เก็บไฟล์ไว้ที่โฟลเดอร์ public/uploads/
},
filename
:
(
req
,
file
,
cb
)
=>
{
cb
(
null
,
Date
.
now
()
+
path
.
extname
(
file
.
originalname
));
// ตั้งชื่อไฟล์ให้ไม่ซ้ำกัน
}
});
//
const storage = multer.diskStorage({
//
destination: (req, file, cb) => {
//
cb(null, 'public/uploads/'); // เก็บไฟล์ไว้ที่โฟลเดอร์ public/uploads/
//
},
//
filename: (req, file, cb) => {
//
cb(null, Date.now() + path.extname(file.originalname)); // ตั้งชื่อไฟล์ให้ไม่ซ้ำกัน
//
}
//
});
// กำหนดเงื่อนไขการอัปโหลดไฟล์
const
upload
=
multer
({
storage
:
storage
,
limits
:
{
fileSize
:
5
*
1024
*
1024
},
// จำกัดขนาด 5MB
fileFilter
:
(
req
,
file
,
cb
)
=>
{
const
fileTypes
=
/jpeg|jpg|png|gif/
;
const
extName
=
fileTypes
.
test
(
path
.
extname
(
file
.
originalname
).
toLowerCase
());
const
mimeType
=
fileTypes
.
test
(
file
.
mimetype
);
// // กำหนดเงื่อนไขการอัปโหลดไฟล์
// const upload = multer({
// storage: storage,
// limits: { fileSize: 5 * 1024 * 1024 }, // จำกัดขนาด 5MB
// fileFilter: (req, file, cb) => {
// const fileTypes = /jpeg|jpg|png|gif/;
// const extName = fileTypes.test(path.extname(file.originalname).toLowerCase());
// const mimeType = fileTypes.test(file.mimetype);
// if (extName && mimeType) {
// return cb(null, true);
// } else {
// return cb(new Error('Only images are allowed!'));
// }
// }
// });
if
(
extName
&&
mimeType
)
{
return
cb
(
null
,
true
);
}
else
{
return
cb
(
new
Error
(
'
Only images are allowed!
'
));
}
}
});
module
.
exports
=
router
;
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment