Get extension from a file url

Issue

I am using file(input) to select a file. I want to retrieve the file extension(.jpeg) from a URL C:\Users\Rajat\Pictures\pic1.jpeg. Then after that I want to put the URL into a text box according to their format i.e audio, video and image.

Solution

In that case you better go with mimetype instead of extensions.

var fileInput = document.getElementById('your-file-input-id');
var fileType = fileInput.files[0].type;
console.log(fileType);//gives image/jpeg or audio/mp3
var parts = filetype.split('/');
parts[0];//image
parts[1];//jpeg

Answered By – SWilk

Answer Checked By – Terry (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.