Im creating a file upload function in node.js with express 3.
I would like to grab the file extension of the image. so i can rename the file and then append the file extension to it.
app.post('/upload', function(req, res, next) {
var is = fs.createReadStream(req.files.upload.path),
fileType = >>>> I want to get the extension of the image here <<<<,
os = fs.createWriteStream('public/images/users/' + req.session.adress + '.' + fileType);
});
How can i get the extension of the image in node.js?
I believe you can do the following to get the extension of a file name.
var path = require('path')
path.extname('index.html')
// returns
'.html'
I'm using this function to get a file extension, because I didn't find a way to do it in an easier way (but I think there is) :
function getExtension(filename) {
var ext = path.extname(filename||'').split('.');
return ext[ext.length - 1];
}
you must require 'path' to use it.
another method which does not use the path module :
function getExtension(filename) {
var i = filename.lastIndexOf('.');
return (i < 0) ? '' : filename.substr(i);
}
// you can send full url here
function getExtension(filename) {
return filename.split('.').pop();
}
If you are using express please add the following line when configuring middleware (bodyParser)
app.use(express.bodyParser({ keepExtensions: true}));
var fileName = req.files.upload.name;
var arr = fileName.split('.');
var extension = arr[length-1];
It's a lot more efficient to use the substring()
method instead of split()
& pop()
Have a look at the performance differences here: http://jsperf.com/remove-first-character-from-string
// returns: 'html'
var path = require('path');
path.extname('index.html').substring(1);
path.extname
will do the trick in most cases. However, it will include everything after the last .
, including the query string and hash fragment of an http request:
var path = require('path')
var extname = path.extname('index.html?username=asdf')
// extname contains '.html?username=asdf'
In such instances, you'll want to try something like this:
var regex = /[#\\?]/g; // regex of illegal extension characters
var extname = path.extname('index.html?username=asdf');
var endOfExt = extname.search(regex);
if (endOfExt > -1) {
extname = extname.substring(0, endOfExt);
}
// extname contains '.html'
Note that extensions with multiple periods (such as .tar.gz
), will not work at all with path.extname
.
A simple solution without need for require which solves the multiple period extension problem:
var filename = 'file.with.long.extension';
var ext = filename.substring(filename.indexOf('.'));
//ext = '.with.long.extension'
Or if you don't want the leading dot:
var filename = 'file.with.long.extension';
var ext = filename.substring(filename.indexOf('.')+1);
//ext = 'with.long.extension'
Make sure to test that the file has an extension too.
var Url = require('url');
var Path = require('path');
var path = 'http://i.imgur.com/Mvv4bx8.jpg?querystring=true';
var result = Path.extname(Url.parse(url).pathname); // '.jpg'