Skip to content

Commit

Permalink
File Successfully Unlinked From Storage
Browse files Browse the repository at this point in the history
  • Loading branch information
pronazmul committed Jun 9, 2021
1 parent f58de96 commit 0e2b8ef
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
2 changes: 1 addition & 1 deletion middlewares/users/avatarUpload.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const avatarUpload = (req, res, next) => {
);

// Call The Multer Upload MiddleWare Function To Handle Error:
upload.any()((req, res, error) => {
upload.any()(req, res, (error) => {
if (error) {
res.status(500).json({
errors: {
Expand Down
2 changes: 1 addition & 1 deletion middlewares/users/userValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const addUserValidationHandler = (req, res, next) => {
if (req.files.length > 0) {
const { filename } = req.files[0];
unlink(
path.join(`${__dirname}/../public/uploads/avatars/${filename}`),
path.join(__dirname, `/../../public/uploads/avatars/${filename}`),
(err) => {
if (err) console.log(err);
}
Expand Down
2 changes: 2 additions & 0 deletions utilities/singleUploader.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const singleUploader = (
destination: (req, file, cb) => {
cb(null, UPLOADS_FOLDER);
},

filename: (req, file, cb) => {
const fileExt = path.extname(file.originalname);
const fileName =
Expand All @@ -28,6 +29,7 @@ const singleUploader = (
.join("-") +
"-" +
Date.now();

cb(null, fileName + fileExt);
},
});
Expand Down
27 changes: 18 additions & 9 deletions views/partials/add_user_modal.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,26 @@
method="post"
action="/users"
enctype="multipart/form-data"
id="add-user-form">
id="add-user-form"
>
<input type="text" placeholder="enter name" name="name" />
<p class="error name-error">This is error</p>
<p class="error name-error"></p>

<input type="text" placeholder="enter email" name="email" />
<p class="error email-error">This is error</p>
<p class="error email-error"></p>

<input type="text" placeholder="enter mobile" name="mobile" />
<p class="error mobile-error">This is error</p>
<p class="error mobile-error"></p>

<input
type="password"
placeholder="enter password"
name="password"
/>
<p class="error password-error">This is error</p>
<p class="error password-error"></p>

<input type="file" name="avatar" />
<p class="error avatar-error">This is error</p>
<p class="error avatar-error"></p>

<p class="error common-error">This is error</p>

Expand All @@ -50,7 +55,7 @@
form.onsubmit = async function(event){
event.preventDefault()
// Clear Field Errors First:
// Clear Errors First:
const errorPlaceHolders = document.querySelectorAll("p.error");
for(let i=0; i<errorPlaceHolders.length; i++){
errorPlaceHolders[i].style.display="none";
Expand All @@ -67,14 +72,18 @@
const formData = new FormData(form);
// Send the request to the server
let response = await fetch("/users",{
let response = await fetch("/users", {
method:"POST",
body: formData
// headers:{'Content-Type': 'multipart/form-data'},
body: formData,
})
// Get Response From Server:
let result = await response.json();
// Logging Errors:
console.log(result.errors)
// Handle Errors and response:
if(result.errors){
// Errors
Expand Down

0 comments on commit 0e2b8ef

Please sign in to comment.