Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 保存多图像多路复用器_Html_Node.js_Multer - Fatal编程技术网

Html 保存多图像多路复用器

Html 保存多图像多路复用器,html,node.js,multer,Html,Node.js,Multer,我在使用Multer保存多个图像时遇到问题 <form action="/api/imagesSection" method="post" enctype="multipart/form-data"> <input type="file" name="uploadedImages" value="uploading_img" multiple> <input type="submit" value="uploading_img">

我在使用Multer保存多个图像时遇到问题

    <form action="/api/imagesSection" method="post" enctype="multipart/form-data">
      <input type="file" name="uploadedImages" value="uploading_img" multiple>
      <input type="submit" value="uploading_img">
    </form>

路线如下:

router.post('/imagesSection', upload.array('uploadedImages', 10), function(req, res) {
        let timeInMs = Date.now();

        for(var i = 0; i < req.files.length; i++){
            let imageS = new ImageS();
            let file = req.files[i].destination + '/' + timeInMs +req.files[i].originalname ;
            imageS.imageUrl = req.files[i].destination + '/' + timeInMs +req.files[i].originalname ;
            imageS.title = 'req.body.title';
            imageS.description = 'req.body.description';

            fs.rename(req.files[i].path, file, function(err) {
                if (err) {
                    console.log(err);
                    res.send(500);
                } else {
                    imageS.save();
                    // res.redirect(req.get('referer'));
                    console.log(imageS);
                }   

            });
        }
    });
router.post('/imagesSection',upload.array('uploadedImages',10),函数(req,res){
让timeInMs=Date.now();
对于(变量i=0;i
如您所见,我在每个循环中调用'save(),一个新图像();
只要保存第一个IMG文件就可以了。有什么想法吗?

可能是因为您的所有文件都具有相同的
原始名称
,而且由于
timeInMs
仅声明一次,因此在for循环之前,它不能作为唯一标识符工作


您是否尝试记录req.files.length的值?它是否返回您期望的结果

编辑:您可以尝试将所有所需文件添加到数组中。然后,您可以使用
.create()
在一个函数调用中保存数组中的所有项,如下所示:

ImageS.create(fileArray, function (err) {
  if (err) // ...
});

下面的代码是异步工作的,这就是为什么只保存一个图像

         fs.rename(req.files[i].path, file, function(err) {
            if (err) {
                console.log(err);
                res.send(500);
            } else {
                imageS.save();
                // res.redirect(req.get('referer'));
                console.log(imageS);
            }   });
不要使用for循环,而是尝试使用forEach(),就像我在代码中所做的那样

var images = [];
req.files.forEach(function(value, index){
        console.log(req.files.length);
        var tempPath = value.path;
        var targetPath = path.resolve('public/img/flat_images/'+flat._id+index+'.'+value.originalname.split('.').slice(-1).pop());
        images.push('/img/flat_images/'+flat._id+index+'.'+value.originalname.split('.').slice(-1).pop());
        console.log(images);
        fs.rename(tempPath,targetPath,function(err){
            if(err) return res.status(400).end();
        });
    });

flat.images = images;
 flat.save(function(err){
   if(err) return res.status(400).end();
   res.send(flat);
 });

希望,它会有帮助。

它会回报你所期望的吗?是的,我已经检查过了,我必须试试你说的。我的朋友没有偷同样的问题。我想
图像
是猫鼬模型吗?您可以编写一个.then()函数来记录每个成功保存到数据库的操作。这可能会给你一些见解。我认为您遇到了问题,因为Mongoose.save()是一个异步IO方法。您必须使用async.each或批添加带有.create()的项好的,我有点不对劲了,请告诉我,什么是fileArray?创建(文件数组,函数(err){if(err)/…});