Join the AI Workshop and learn to build real-world apps with AI. A hands-on, practical program to level up your skills.
When handling file uploads with multipart/form-data, the server receives file objects (e.g. in req.files in Express).
Each file object includes path, name, size, type, and similar fields:
{
logo: File {
size: 121920,
path: '/var/folders/tn/h8lfq1sj7c33c0p30qgkd3mw0000gn/T/upload_b9e85b7cf989482a1760d82b77fd555a',
name: 'Screen Shot 2021-06-07 at 21.40.29.png',
type: 'image/png',
hash: null,
lastModifiedDate: 2021-06-07T22:20:50.150Z,
//...
}
}
The temporary file path may not include an extension. To get the extension you can either parse the original file name:
const path = require('path')
path.extname(req.files.logo.name) //.png
This requires no extra packages. Or use the mime-types package and derive the extension from the MIME type:
const mime = require('mime-types')
mime.extension('text/plain') //txt
mime.extension('image/png') //png