【问题标题】:Multiple files not getting uploaded to the server in node.js多个文件未在 node.js 中上传到服务器
【发布时间】:2017-01-01 11:55:47
【问题描述】:

正在尝试使用multiparty 在服务器中上传多个文件。在服务器端获取两个文件,但在写入时只上传单个文件。当我调试代码时,我发现在文件读取发生之前,控件正在返回 for 循环的开头。因此,仅处理对象中的最新文件以供上传。为什么会这样?下面是相同的代码。

form.parse(req, function(err, fields, files){   
          var fileArry=files.uploadFiles;
                if(fileArry.length == 0){
                    console.log(" No file found to  upload !!!");
                    res.send('No files found to upload.');
                            return; 
                }
                for(var i=0; i<fileArry.length ; i++)
                {  
                    newPath='./uploads/';
                    singleFile=fileArry[i];

                    console.log("::::::::::::::::::::: This is the single file and it  path ::::::::::::::::::::::::");                         
                    console.log(singleFile);

                    newPath+=singleFile.originalFilename;

                    console.log("::::::::::::::::::::: New file path is :"+newPath)         
                    console.log("::::::::::::::::::::: Going inside file  :::::::::::::::::::::::::::::::::::::::::::");
                        
                    fs.readFile(singleFile.path, (err, data) => {
                       fs.writeFile(newPath, data, (err) => {                       
                         console.log("Files uploaded "+newPath);
                                    });
                                });                                 
                          }
                        res.send("File uploaded to: " + newPath);
                });

调试后的结果是 -

如何克服这个问题并一次上传多个文件?

【问题讨论】:

    标签: javascript node.js file file-upload


    【解决方案1】:

    在尝试了几个小时并进行了多次谷歌搜索后,link 遇到了类似的情况,是的,它确实解决了我的问题。根据链接,更改后的代码如下,它可以用于上传多个文件。

    app.post('/multiFileUpload', function(req, res) {
    
        var singleFile;     
        var form = new multiparty.Form();
    
       form.parse(req, function(err, fields, files){    
          var fileArry=files.uploadFiles;                   
                    if(fileArry.length == 0){
                        res.send('No files found to upload.');
                            return; 
                    }
    
                        for(i=0; i<fileArry.length; i++)
                        {  
                            newPath='./uploads/';
                            singleFile=fileArry[i];
                            newPath+=singleFile.originalFilename;
                            readAndWriteFile(singleFile,newPath);                       
                        }
                        res.send("File uploaded to: " + newPath);
                });
    
    });
    
     function readAndWriteFile(singleFile , newPath){
    
        fs.readFile(singleFile.path, (err, data)=>{
            fs.writeFile(newPath, data, (err)=>{                                                                                                                
                    console.log("File uploaded to  :"+newPath);
                });
        });
    }
    

    【讨论】:

      猜你喜欢
      • 2014-09-21
      • 2019-06-15
      • 2015-02-27
      • 1970-01-01
      • 1970-01-01
      • 2012-10-27
      • 1970-01-01
      • 1970-01-01
      • 2020-08-31
      相关资源
      最近更新 更多