This is the app.post method in the index.js file which will receive the filename and I want to check if the image exists then the image name will be added to the file else it will not.
<code>app.post('/addPost', upload.single('blogImage'), (req, res) => {
//for the written content
let data = req.body;
data.blogImageName = req.file.filename;
console.log(data);
postContent(req.body);
});
</code>
<code>app.post('/addPost', upload.single('blogImage'), (req, res) => {
//for the written content
let data = req.body;
data.blogImageName = req.file.filename;
console.log(data);
postContent(req.body);
});
</code>
app.post('/addPost', upload.single('blogImage'), (req, res) => {
//for the written content
let data = req.body;
data.blogImageName = req.file.filename;
console.log(data);
postContent(req.body);
});
This is the html form
<code><form action="/addPost" enctype="multipart/form-data" method="POST" class="createPostForm">
<div>
<label for="AddImage">Add Image for the blog : </label>
<input type="file" name="blogImage" ><br>
</div>
<input type="submit" value="Add Post">
</form>
</code>
<code><form action="/addPost" enctype="multipart/form-data" method="POST" class="createPostForm">
<div>
<label for="AddImage">Add Image for the blog : </label>
<input type="file" name="blogImage" ><br>
</div>
<input type="submit" value="Add Post">
</form>
</code>
<form action="/addPost" enctype="multipart/form-data" method="POST" class="createPostForm">
<div>
<label for="AddImage">Add Image for the blog : </label>
<input type="file" name="blogImage" ><br>
</div>
<input type="submit" value="Add Post">
</form>
if i submit the form without uploading file then it gives an error.
How can i check if filename exists in the request?
New contributor
Vedica Gairola is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.