-
-
Save asci/6d4891f1caa624c9a3ce to your computer and use it in GitHub Desktop.
Upload fail
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.use(function (req, res, next) { | |
var files = req.files; | |
if (req.method.toLowerCase() === 'post' && req.user && files) {// only for logged in users | |
var counter = 0; | |
for (var file in files) { | |
counter++; | |
var filename = Math.random().toString(16).substr(2) + Date.now().toString(16); | |
filename += '.' + files[file].name.split('.').pop(); | |
fs.rename(files[file].path, __dirname + '/public/uploads/' + filename, (function (file, filename) { | |
return function (err) { | |
if (err) { | |
console.log(err); | |
} else { | |
req.body[file] = filename; | |
} | |
if (!--counter) { | |
next(); | |
} | |
} | |
}(file, filename))); | |
} | |
} else { | |
next(); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment