0

In my application user can upload multiple images at same time. I am assigning name to uploaded image as current timestamp. This is my code :

let base64Data = reqarr[i].imgname.replace('data:image/' + imgext + ';base64,', "");
require("fs").writeFile("../UI/client/" + new Date().getTime() + userid, base64Data, 'base64', function(err) {
  if (err) {
    console.log(err);
    logger.error(common.getErrorMessageFrom(err));
  }

});

Is it possible that same timestamp will get assigned to 2 different images?

mplungjan
  • 169,008
  • 28
  • 173
  • 236
AJS
  • 953
  • 7
  • 21
  • yes, you might want to add a user id or something else to the timestamp – manonthemat Dec 08 '16 at 07:10
  • It is possible but without seeing your code we have no chance of knowing – mplungjan Dec 08 '16 at 07:14
  • @manonthemat yes i am also adding username also . But if 2 people are using same account and and they logged in from 2 different places and uploaded image at same time then ? – AJS Dec 08 '16 at 07:16
  • @mplungjan that answer is wrong . i have tried same thing but it can get same value. Check this console.log(new Date().getTime() + Math.floor(Math.random())+"--"+new Date().getTime() + Math.floor(Math.random())); – AJS Dec 08 '16 at 07:43
  • `var uniqueName = new Date().getTime()+Math.random(); ........... var actualTS = parseInt(unuqueName);` - Math.floor(Math.random()) is always 0 – mplungjan Dec 08 '16 at 07:47
  • yes but i can't give image name with DOT(.) in name so it doesn't help me. This is answer --> Yes. The value returned from new Date().getTime() is an integer (i.e. fractionless number), while the result of Math.random() is always a real number between 0 and 1, excluding 1. So, just discard the fraction (e.g. using Math.floor()) and then you have the numeric timestamp that can be used to construct the Date object again this is written there – AJS Dec 08 '16 at 08:04

0 Answers0