You can get extension of uploaded file in jquery. There are many ways to get extension of file in jquery.
fileName.split('.').pop() is the best method to complete this work.
You can also use fileName.split('.')[1] method, but if file name already contain "." then this code will not work
$('.btnClick').click( function() {
if ($('.flUpload').val() != '') {
var file = $('.flUpload')[0].files[0];
var fileName = file.name;
var fileExt = '.' + fileName.split('.').pop();
alert(fileExt);
}
else {
alert('Please select a file.')
}
});
CODE
<!DOCTYPE html> <html> <head> <title>jQuery With Example</title> <script src="http://code.jquery.com/jquery-1.9.1.js" type="text/javascript"></script> <script type="text/javascript"> $(function () { $('.btnClick').click(function () { if ($('.flUpload').val() != '') { var file = $('.flUpload')[0].files[0]; var fileName = file.name; var fileExt = '.' + fileName.split('.').pop(); alert(fileExt); } else { alert('Please select a file.') } }); }); </script> </head> <body> <div class="Container"> <input type="file" class="flUpload" /> <button type="button" class="btnClick">Click</button><br /> Please upload any file and click the button to get the extension of uploaded file. </div> </body> </html>
DISPLAY
Please upload any file and click the button to get the extension of uploaded file.
No comments :
Please Give Your Feedback