Skip to content

Instantly share code, notes, and snippets.

@asci
Created December 28, 2013 19:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asci/6d4891f1caa624c9a3ce to your computer and use it in GitHub Desktop.
Save asci/6d4891f1caa624c9a3ce to your computer and use it in GitHub Desktop.
Upload fail
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