Skip to content

Instantly share code, notes, and snippets.

@smhatre59
Last active October 23, 2018 23:43
Show Gist options
  • Save smhatre59/7edc6dd068a2ad3cd5c60a049cd870be to your computer and use it in GitHub Desktop.
Save smhatre59/7edc6dd068a2ad3cd5c60a049cd870be to your computer and use it in GitHub Desktop.
file upload module
var fs = require("fs");
var path = require('path');
var writePath = '/home/saurabh/Documents/';
var cmd=require('node-cmd');
var async = require('async');
var jsonfile = require('jsonfile');
exports.fileupload = function(req,res){
// console.log("req",req.files);
var filesArray = req.files;
async.each(filesArray,function(file,eachcallback){
async.waterfall([
function (callback) {
fs.readFile(file.path, (err, data) => {
if (err) {
console.log("err ocurred", err);
}
else {
callback(null,data);
}
});
},
function (data, callback) {
fs.writeFile(writePath + file.originalname, data, (err) => {
if (err) {
console.log("error occured", err);
}
else {
callback(null, 'success');
}
});
},
function (arg1, callback) {
var filepath = './userdata/userid.json'
jsonfile.readFile(filepath, function(err, obj) {
callback(null,'done')
})
}
], function (err, result) {
// result now equals 'done'
//pass final callback to async each to move on to next file
eachcallback();
});
},function(err){
if(err){
console.log("error ocurred in each",err);
}
else{
console.log("finished prcessing");
res.send({
"code":"200",
"success":"files printed successfully"
})
cmd.run('rm -rf ./fileupload/*');
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment