Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
169 views
in Technique[技术] by (71.8m points)

javascript - Unable to upload images to a specific folder in node.js using fileuploads

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' }


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You're using mkdirp synchronously, while it's an asynchronous operation.

Either move all your code inside then(made=>{ ... }) or use async await.

router.post("/addProduct",async (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
  })
  
  await nProduct.save();

  await mkdirp("public/product_images/"+nProduct._id);
  await mkdirp("public/product_images/"+nProduct._id+"/gallery");
  await mkdirp("public/product_images/"+nProduct._id+"/gallery/thumbs");

  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)
    })
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...