3

Was using multiparty node module in node app for uploading a single file. Now, I want to upload multiple files using the same multiparty module.I googled but could not find any solution and ended up finding 'multer' module in the link which is giving some issue with the existing application. So, is there any way to achieve uploading of file using 'multiparty' ?

2 Answers 2

2

After many failed attempts and experimentation got the answer, have sent a form object to the server from the client. Just have a check of the below code on the server side

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 == null ){
                        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);
                });
        });
    }
Sign up to request clarification or add additional context in comments.

1 Comment

I did the very same than you and did just upload only the last file. The only difference was that I did the readFile and fs.writeFile inside the For. I don't know why it wasn't working. Anywa thanx! :D @Worker
0

I recommend the request module. It will help you solve your problem.

1 Comment

Thanks @David, an example using the same would be more helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.