简体   繁体   English

无法使用文件上传将图像上传到 node.js 中的特定文件夹

[英]Unable to upload images to a specific folder in node.js using fileuploads

Below is the html for file upload下面是用于文件上传的 html

<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使用fileupload模块在 NodeJS 中处理图像上传的服务器端代码

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:\NodeE\public\product_images\5ff718bebe310d2f3c34590a\noimage.jpg'] { errno: -4058, code: 'ENOENT', syscall: 'open', path: 'D:\NodeE\public\product_images\5ff718bebe310d2f3c34590a\noimage.jpg' } [错误:ENOENT:没有这样的文件或目录,打开 'D:\NodeE\public\product_images\5ff718bebe310d2f3c34590a\noimage.jpg'] { errno:-4058,代码:'ENOENT',系统调用:'open',路径:' D:\NodeE\public\product_images\5ff718bebe310d2f3c34590a\noimage.jpg' }

You're using mkdirp synchronously, while it's an asynchronous operation.您正在同步使用 mkdirp,而它是异步操作。

Either move all your code inside then(made=>{... }) or use async await.将所有代码移入then(made=>{... })或使用异步等待。

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

You need to chain the then 's so that you are writing to the directory only after it is created您需要链接then ,以便仅在创建目录后才写入目录

Eg.例如。

mkdirp(...)
  .then(made=>{
    // write to directory here
  })

Or if you want to write after creating all the directories use或者,如果您想在创建所有目录后编写使用

Promise.all(mdirp('...'), mdirp('...'), mdirp('...'))
  .then(made=>{
    // write to directory here
  })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM