I searched over the internet but I don't find a solution. I use Nodejs Formidable to handle uploaded files ans save it into my API.
All work fine but i want to know how I can change the filename with a unique filename (example: Timestamp) (while maintaining the original file extension).
Here is the code I use :
form.on('end', function (fields, files) {
var temp_path = this.openedFiles[0].path;
var file_name = this.openedFiles[0].name;
var new_location = GLOBAL.config.uploadDir;
fs.move(temp_path, new_location + file_name, {clobber:true}, function (err) {
if (err) {
console.error(err);
fs.unlink(temp_path, function (err) {
if (err) {
console.error(err);
console.log("TROUBLE deletion temp !");
} else {
console.log("success deletion temp !");
}
});
} else {
res.json('created');
}
});
});
Can any body suggest a solution on this...please?
I found a solution :-) First require path :
var path = require('path');
Then I do the trick like this :