Learn, Share, Build

260
September 28, 2017, at 9:04 PM

i have sent list of files to the node server using form data.now i want to upload them 1 by one using formidable but i don,t know how to iterate over files

Router.post('/Registration', (req, res) => {
    var hospital =new hospitals();
    var hospitaldata={};
    var form = new formidable.IncomingForm();
    form.parse(req, function (err, fields, files) {
        hospitaldata=JSON.parse(fields.hospitaldata);
        console.log(util.inspect({fields: fields, files: files}));
        if(files !=null){
            for (var i = 0; i < files.length; i++) {
                var newpath="";
           var oldpath = files[i].File.path;
           newpath = 'C:/Users/saths/Desktop/expressjs-sample/expressjs-sample/uploads' + files[i].File.name;

          fs.rename(oldpath, newpath, function (err) {
             if (err) throw err;

           });
              hospital.img.push({data:fs.readFileSync(newpath),contentType: 'image/png'});
        }}
        hospital.name = hospitaldata.name;
        hospital.address = hospitaldata.address;
        hospital.geolocation = hospitaldata.geolocation;
        hospital.phones = hospitaldata.phones;

        hospital.save(function (err) {
            if (err) {
                console.log(err.message);
                res.json({success: false, message: 'User information already exsits'});
            }
            else {
                res.json({success: true, message: "user created"});
            }
        });
    });

});

this is how i send files to the back end using form data

var files = $scope.files;
var fd = new FormData();

for (var i = 0; i < files.length; i++) {
    fd.append("file"+i, files[i])
}

fd.append('hospitaldata', JSON.stringify(hospital));
Rent Charter Buses Company
READ ALSO
Learn, Share, Build

Learn, Share, Build

I'm using mssql(nodejs)

252
Learn, Share, Build

Learn, Share, Build

I'm using the following library to do: http://maboiteaspamgithub

254
Learn, Share, Build

Learn, Share, Build

I am making an application on using Nodejs and angular as fronthand

227