I'm trying to upload files to the server in this way (below are parts of three files, a router, a controller and a script that loads the files):
///router var multipart = require("connect-multiparty"); var multipartMiddleware = multipart(); //..... router.post("/save", multipartMiddleware, function(req, res, next) { var s = new Settings(res,req); s.saveSection(); }); //controller var File = require('fileuploader'); //more action this.saveSection = function(){ //more action var files = new File(req.files); log.info(req.files); files.moveUploadedFiles(FOLDER,function(upload){ //moreactions... }); }; //uploader //moreactions.... this.moveUploadedFiles = function(dest,cb){ async.waterfall ([ function (cb) { $this.createFolder(dest,function(){ cb(null); }); } ], function () { var infile = []; var outfile = []; var uploadSize = []; for (var i in files) { (function(i) { var path = dest + '/' + files[i].originalFilename; infile[i] = fs.createReadStream(files[i].path); outfile[i] = fs.createWriteStream(path); infile[i].on('data',function(data) { if(!uploadSize[i]){ uploadSize[i] = 0; } uploadSize[i] += data.length/1024; console.log(files[i].originalFilename + ': '+parseInt(uploadSize[i])+'/'+(parseInt(files[i].size/1024)) + ' Kb'); }); infile[i].pipe(outfile[i]); })(i); } return cb(1); }); };
The bottom line is that the files are first loaded, and then the answer comes and I see in the console the progress of copying them from the temporary directory to the target one. How to make stream at the input somewhere in the controller or at the router level, I don’t know which is better, but in my opinion it’s better to do it in the uploader script. Those. the task is that at the moment of submission of the form, the download starts immediately with the help of stream, so that this progress can be output, but not like now - I can see the progress of copying the file already when it is on the server ... How to implement it? Maybe within the same request, the transferred files have the ability to read asynchronously? or is it not possible?