Node.js get file extension

后端 未结 13 993
后悔当初
后悔当初 2020-12-23 00:16

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

13条回答
  •  青春惊慌失措
    2020-12-23 00:40

    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.

提交回复
热议问题