📜  上传多张图片 cloudinary - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:04:22.940000             🧑  作者: Mango

代码示例1
const cloudinaryImageUploadMethod = async file => {
      return new Promise(resolve => {
          cloudinary.uploader.upload( file , (err, res) => {
            if (err) return res.status(500).send("upload image error")
              console.log( res.secure_url )
              resolve({
                res: res.secure_url
              }) 
            }
          ) 
      })
    }
    
    router.post("/", [auth_middleware, upload.array("img", 3 )], async (req, res) => {

        const urls = [];
        const files = req.files;
        for (const file of files) {
          const { path } = file;
          const newPath = await cloudinaryImageUploadMethod(path)
          urls.push(newPath)
        }
        
        const product = new Product({
          u_id: req.user._id,  
          name: req.body.name,
          description: req.body.description,
          productImages: urls.map( url => url.res ),
        });

     }