Below is the html for file upload
<div class="form-group" >
<label for="Image">Image</label>
<input type="file" name="image" value="<%=fillData.image%>" id="inpFile" class="form-control"placeholder="drop an image " /><br>
<div class="image-preview" style="text-align: center;">
<img src="" id="imgPreview" alt="imagePreview"/>
</div>
</div>
The server side code for handling the image uplaod in NodeJS using fileupload
module
router.post("/addProduct",(req,res)=>{
const nimage = req.files.image.name
const Price = parseFloat(req.body.price).toFixed(2)
console.log(req.files.image)
const nProduct = new Products({
title:req.body.title,
slug:req.body.slug,
desc :req.body.description,
category:req.body.category,
price:Price,
image:nimage
})
nProduct.save().then((value) => {
mkdirp("public/product_images/"+nProduct._id).then(made=>{
console.log(`file created starting with on id ${made}`)
})
mkdirp("public/product_images/"+nProduct._id+"/gallery").then(made=>{
console.log(`file created starting with id and gallery ${made}`)
})
mkdirp("public/product_images/"+nProduct._id+"/gallery/thumbs").then(made=>{
console.log(`file created starting with and thumbs${made}`)
})
if(nimage!=""){
console.log("hello")
const productImage = req.files.image
const path = "public/product_images/"+nProduct._id+"/"+nimage;
console.log(path)
productImage.mv(path, function(err){
return console.log(err)
})
}
})
}
Getting the following error
[Error: ENOENT: no such file or directory, open 'D:NodeEpublicproduct_images5ff718bebe310d2f3c34590a
oimage.jpg'] {
errno: -4058,
code: 'ENOENT',
syscall: 'open',
path: 'D:NodeEpublicproduct_images5ff718bebe310d2f3c34590a
oimage.jpg'
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…