EN
Express.js - how to give files unique names on upload?
2 answers
3 points
I need unique names for files in my application, so the filenames won't double. How can I do that?
2 answers
7 points
Try to use embedded Node.js crypto
module.
e.g.
xxxxxxxxxx
1
const crypto = require('crypto');
2
3
// Example outout:
4
5
console.log(crypto.randomUUID()); // 12e9e08c-8710-4c08-bef2-60915debcf40
6
console.log(crypto.randomUUID()); // f8844934-0fcb-43a1-b463-13f9c11775cd
7
console.log(crypto.randomUUID()); // edea8f66-48e8-4fb9-8b8c-1e6a929f847b
8
console.log(crypto.randomUUID()); // 52a0a4b9-18b6-4dd0-a8a3-61e839f6300a
9
10
11
// Note: Added in some Node.js minor versions: v15.6.0 and v14.17.0
Source: https://dirask.com/snippets/Node-js-generate-random-UUID-embedded-native-solution-pONAZD
0 commentsShow commentsAdd comment
3 points
You can use uuid package like this:
xxxxxxxxxx
1
const { v4: uuidv4 } = require('uuid'); // npm install uuid
2
3
const filename = uuidv4() + '.txt';
This should give you example filename
like:
xxxxxxxxxx
1
0c8c72a0-c346-4f4b-b39e-5548d490a5fa.txt
See also
References
0 commentsAdd comment