Reputation: 417
I am new to MEAN development. I have two input fields and one field with a photo upload button. While I managed to display uploaded photo on the screen, I have a problem uploading it on server. Can anyone help?
I also get this error:
Error: ENOENT: no such file or directory, open 'C:\Something\Something\Something\Practice\finalappbackend\backedn\images\title-i-entered-1623300156286.jpeg'
Here's my code on backend:
app.js
const multer = require("multer");
const MIME_TYPE_MAP = {
"image/png": "png",
"image/jpeg": "jpeg",
"image/jpg": "jpg",
};
const storage = multer.diskStorage({
destination: (req, file, cb) => {
const isValid = MIME_TYPE_MAP[file.mimetype];
let error = new Error("Invalid mime type");
if (isValid) {
error = null;
}
cb(error, "backedn/images");
},
filename: (req, file, cb) => {
const name = file.originalname.toLowerCase().split(" ").join("-");
const ext = MIME_TYPE_MAP[file.mimetype];
cb(null, name + "-" + Date.now() + "." + ext);
},
});
app.post(
"/api/posts",
multer({ storage: storage }).single("image"),
(req, res, next) => {
const post = new Post({
title: req.body.title,
content: req.body.content,
});
console.log(post);
post.save().then((result) => {
res.status(201).json({
message: "Post added successfully",
postId: result._id,
});
});
}
);
Here's my code on Frontend:
posts.service.ts
addPost(id: string, title: string, content: string, image: File) {
const postData = new FormData();
postData.append("title", title);
postData.append("content", content);
postData.append("image", image, title);
this.http
.post<{ message: string; postId: string }>(
'http://localhost:3000/api/posts',
postData
)
.subscribe((responseData) => {
const post: Post = {id: responseData.postId, title: title, content: content}
this.posts.push(post);
this.postsUpdated.next([...this.posts]);
});
}
post-create.component.ts
imagePreview: string | ArrayBuffer;
onImagePicked(event: Event) {
const file = (event.target as HTMLInputElement).files[0];
this.form.patchValue({ image: file });
this.form.get('image').updateValueAndValidity();
const reader = new FileReader();
reader.onload = () => {
this.imagePreview = reader.result;
};
reader.readAsDataURL(file);
}
onSavePost() {
if (this.form.invalid) {
return;
}
if (this.mode === 'create') {
this.postsService.addPost(
this.form.value.id,
this.form.value.title,
this.form.value.content,
this.form.value.image
);
} else {
this.postsService.updatePost(
this.postId,
this.form.value.title,
this.form.value.content
);
}
this.form.reset();
}
And post-create.component.html
<div>
<button mat-stroked-button type="button" (click)="filePicker.click()">
Pick Image
</button>
<input type="file" #filePicker (change)="onImagePicked($event)" />
</div>
Upvotes: 0
Views: 3223
Reputation: 5411
ENOENT stands for: Error No Entry. If you look at the error:
Error: ENOENT: no such file or directory, open 'C:\Something\Something\Something\Practice\finalappbackend\backedn\images\title-i-entered-1623300156286.jpeg'
It means: I cannot open '...finalappbackend\backedn\images'
to save the image into, because this directory doesn't exist.
In the multer npm page
Note: You are responsible for creating the directory when providing destination as a function. When passing a string, multer will make sure that the directory is created for you.
Make sure the directory has been created. It could be a typo backedn
in the name.
Upvotes: 1