How to check file extension while uploading a file in multifile.js

2.6k views Asked by At

I need to upload DOC and XLS files to my application. I'm using multifile.js for uploading the file.

I need to prevent uploading files except DOC and XLS - how can i achieve that?

1

There are 1 answers

4
AurA On

This script should be put as a validation before file is submitted. It should also work for filenames which already contains '.'(dot) eg.. myfile.ms.xls etc...

var splitLength = parseInt($('#file').val().split('.').length)
var extensionCaseInsensitive = $('#file').val().split('.')[splitLength-1]    

if (extensionCaseInsensitive.toUpperCase() == 'DOC' || extensionCaseInsensitive.toUpperCase() == 'XLS') {
    // allow upload
}

But remember one thing if a user is literally naming a png image as doc file then we cannot check the contents to validate or probably it will a much longer and complicated exercise.