0

I have a requirement where I have to import a file from the local using javascript jquery. Depending on the type of file that user has selected the displaying logic is used. The flow is such: User selects on 'Import' and selects the file from local. As soon as he says okay I need to retrieve the file extension that has been selected and depending on that need to display the file.

user2450679
  • 15
  • 1
  • 8

2 Answers2

2

Here is the plunker working code

You will be getting the mime type which is the safest way to check. I just made the alert box of mime type of the code.

<form enctype="multipart/form-data">
<input name="file" type="file" />
<input type="submit" value="Upload" />
</form>


<script>
$('input[type=file]').change(function(){
alert('To check whether i am getting into the script '); //Comment this
var file = this.files[0];
name = file.name;
size = file.size;
type = file.type;
alert(type);
//your validation
});
</script>

Credits : Goes To

Community
  • 1
  • 1
Alaksandar Jesus Gene
  • 6,523
  • 12
  • 52
  • 83
2

I have been using the below script Check file extension var file_name="file.jepg"; var extension = /.(\w+)$/.exec(file_name)[1]; if(extension=="condition") {

}
Rakesh Vekariya
  • 574
  • 2
  • 8